# \[Android\] When the application is reconnected, 'setup' operates again

**URL:** <https://discourse.processing.org/t/android-when-the-application-is-reconnected-setup-operates-again/34215>\
**Category:** Processing for Android\
**Created:** [December 20, 2021, 11:33pm UTC](https://discourse.processing.org/t/android-when-the-application-is-reconnected-setup-operates-again/34215 "2021-12-20T23:33:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [December 20, 2021, 11:33pm UTC](https://discourse.processing.org/t/android-when-the-application-is-reconnected-setup-operates-again/34215/1 "2021-12-20T23:33:47Z")

</div>

When the application is reconnected, ‘setup’ operates again.

How to operate ‘SETUP’ only once?

```auto
void setup(){
    s1 = new MediaPlayer();
    s2 = new MediaPlayer();
    
    af1 = context.getAssets().openFd("1.mp3");
    af2 = context.getAssets().openFd("2.mp3");

    s1.setDataSource(af1.getFileDescriptor(), af1.getStartOffset(), af1.getLength());
    s2.setDataSource(af2.getFileDescriptor(), af2.getStartOffset(), af2.getLength());

    s1.prepare();
    s2.prepare();
}

```

1. Run the created ‘APP’ on your smartphone. It works normally.

2. It appears on the background screen for a while, then reconnects to the ‘APP’.

3. ‘setup’ restarts, so it seems that mp3 files are duplicated twice in s1 and af1.

4. The same sound source is played twice.

5. Is there a way to prevent setup from working even after leaving the ‘APP’ screen?

```auto
boolean setup_en = true;
void setup(){

 if(setup_en){ setup_en = false;
    s1 = new MediaPlayer();
    s2 = new MediaPlayer();
    
    af1 = context.getAssets().openFd("1.mp3");
    af2 = context.getAssets().openFd("2.mp3");

    s1.setDataSource(af1.getFileDescriptor(), af1.getStartOffset(), af1.getLength());
    s2.setDataSource(af2.getFileDescriptor(), af2.getStartOffset(), af2.getLength());

    s1.prepare();
    s2.prepare();
  }
}

```

It can’t be solved with the above method, right?

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 21, 2021, 6:49pm UTC](https://discourse.processing.org/t/android-when-the-application-is-reconnected-setup-operates-again/34215/2 "2021-12-21T18:49:29Z")

</div>

You could maybe handle it by saving to a random file during setup. This way you can check if the file exists. If it does then dont call the methods in setup.

Maybe theres a more elegant solution, or maybe you could make use of onload() and migrate the code from setup.

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [December 22, 2021, 1:57pm UTC](https://discourse.processing.org/t/android-when-the-application-is-reconnected-setup-operates-again/34215/3 "2021-12-22T13:57:15Z")

</div>

@paulgoux

Thank you for your interest and reply.

1. As you said, it’s a way to control through storage.  
I saved it in ‘.csv’ and processed it.

2. The second is definitely to close the APP and turn it on again.

```auto
moveTaskToBack(true);
finishAndRemoveTask();
android.os.Process.killProcess(android.os.Process.myPid());

```

I solved it through the corresponding source code.
