I've already been looking for solutions about my problem for much time, but The Android documentation is not so clear and web solutions don't work completely.
I'm developing a game in which the main Activity (where the game takes place) can call another simple Activity and than return back. It would work like this:
For the first two points I tried this to be sure that one and only one main Activity is created and called: android:launchMode="singleTask"
or android:launchMode="singleTop"
but unluckily the first works for the first problem and the second for the second one, not for both!
I think the problem is: with singleTop the main Activity isn't actually on the top of the stack when the second activity calls it (to be clear, I don't understand what should be the situation in which I call an activity that is already on the top of the stack!). Conversely, the singleTask ensures that the Activity is unique in its stack, but after the users click the Home button and returns to the Application the other Task is called, am I right?
Anyway, is this a smart solution or I should save the state of my game in the onStop/onPause and then resume that in the onResume?
For the third point I tried to add the android:noHistory="true"
to every Activity, then when the user clicks the exit button I call a new Activity which do nothing but the finish()
method on the onCreate, this Activity is called with intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
and its launchMode is singleInstance
, but it doesn't work, it simply returns always in the main Activity.
standard and singleTop comes in one side and singleTask and singleInstance comes in another side. The main difference between standard and singleTop is in standard, every time a new intent for standard activity, a new instance is created.
SingleInstance — This is the same as single-task mode but, we cannot launch any other activity inside the task which has an activity launched with single-instance mode.
2. singleTop. In this launch mode if an instance of activity already exists at the top of the current task, a new instance will not be created and Android system will route the intent information through onNewIntent(). If an instance is not present on top of task then new instance will be created.
Single Task In this method of operation, a new task is always generated, and a new instance is added to the task as the root one. If the activity already exists on another task, no new instance is created, and the Android system transmits the intent information via the onNewIntent() function.
You definitely don't want to mess with the launchMode
. That isn't the way to solve this problem and will just cause you more problems.
Android can (and will) kill your process whenever it wants to (but especially if your application is in the background). Because of this, you need to save the state of your game in onPause()
, because that is the only opportunity that you are guaranteed to have.
Anyway, if you save the game in onPause()
the standard behaviour will cover your first 2 points.
For your 3rd point, there are a few options:
startActivityForResult()
see my answer here
Like this:
Intent intent = new Intent(this, MyRootActivity.class); // this is the starting activity for your application
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // tells Android to finish all other activities in the stack
intent.addExtra("exit", "true"); // this tells your root activity that you want to exit
startActivity(intent);
Now, in your MyRootActivity
you want to add this code to onCreate()
:
Intent intent = getIntent();
if (intent.hasExtra("exit")) {
// User wants to exit
finish();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With