1

My Android App behaves different when i reopen the App via Icon or "Recent Apps Window Collection (This Button with all open windows on Android)"

My Goal:

App is running and i want it to open like i left it when i click the App Icon

I tried changing the AndroidLaunchMode, but the App restarts when i click on the icon. If i open my App over my Recent Apps Window (where i see my current View) it opens at the position i left it. So it should work somehow right?

I have a Cordova Application and i set the AndroidLaunchMode with:

<preference name="AndroidLaunchMode" value="standard" />

<preference name="AndroidLaunchMode" value="singleTask" />

<preference name="AndroidLaunchMode" value="singleTop" />

<preference name="AndroidLaunchMode" value="singleInstance" />

All seem to do the same. I don't even know what the right one would be.

What do i wrong? Or do i understand the AndroidLaunchMode wrong and need to change something else?

UPDATE:

AndroidLaunchMode does not appear in the final AndroidManifest.xml. The problem seems to be something with cordova config.xml file ..

Jonas
  • 2,139
  • 17
  • 38
  • 3
    Does this answer your question? [App completely restarting when launched by icon press in launcher](https://stackoverflow.com/questions/16126511/app-completely-restarting-when-launched-by-icon-press-in-launcher) – yukashima huksay Apr 29 '20 at 04:05

2 Answers2

1

This somehow works:

<gap:config-file platform="android" parent="/manifest/application">
    <activity android:launchMode="singleInstance" />
</gap:config-file>
Jonas
  • 2,139
  • 17
  • 38
0

Launch modes only allows you to define how a new instance of an activity is associated with the current task, this is not the only approach that you are looking for, you will probably have problems later if your app code increases.

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed.

You need to handle your activity state, there are many answers about that, you can take a look, but basically you have to:

1.- Save:

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);


    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

2.- Restore. In the official android documentation it says that you can restore in onCreate and in onRestoreInstanceState, for this approach I suggest you to do it in onRestoreInstanceState, so you don't need to re-create your activity every time:

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);


    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

Please take a look to this Android guide.

Brandon Zamudio
  • 2,853
  • 4
  • 18
  • 34
  • Like i wrote in my own answer: AndroidLaunchMode does the right thing. The problem was just that my activity did not get the AndroidLaunchMode because of a problem with cordova. Your answers answers the question how to save an activity.. But the problem was that the activity ist still active! Thanks for your effort tho. – Jonas Aug 30 '17 at 09:40