Resuming the last action when you click on the start icon

As I can see, most applications resume the last action when they click on the start icon. However, this does not seem to be the default behavior. In my activity, launching an application always starts when you click on the launch icon.

How to configure the application to resume the last action when you click on the start icon, and the application is already running.

+6
source share
6 answers

Problem:

I canโ€™t say that this is a mistake, but this behavior has been happening since the first version of Android. When starting the application from the launcher, there is a problem with tasks and roottasks in releases. You can find the corresponding error report here .

Decision:

You can fix adding the following code to your onCreate of your startup activity:

if (!isTaskRoot()) { finish(); return; } 
+16
source

I met the same problem and solved it by adding android:launchMode="singleInstance" to the application tag inside the manifest. You can check startup mode details in

android: launchMode

+2
source

Are you sure you are not leaving the application? See Also Activity Stitch .

+1
source

It depends on the life cycle of your activity. See the โ€œActivity Life Cycleโ€ on the Android Developer website. If your activity is simply paused, it will resume if the activity returns to the foreground and if you click on the icon.

If you want the user to continue the last action, even if the action was destroyed, you must encode your own solution in order to proceed to the correct action.

0
source

It will be an ugly hack, but you can try the following:

Put a static variable called lastStopped in a subclass of the application (or any singleton, for that matter). Set to null by default.

In onCreate of your first action, you will have something like this:

 if (MyApp.lastStopped != null) { Intent intent = new Intent(this, MyApp.lastStopped.getClass()); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); finish(); return; } 

Now, any action that you want to return after clicking on the house should have:

 @Override protected void onStop() { super.onStop(); MyApp.lastStopped = this; } 

Make sure you clear MyApp.lastStopped when you run another action to prevent a memory leak.

Let me know if this works for you, it was for me!

0
source

You can check the flag: FLAG_ACTIVITY_BROUGHT_TO_FRONT to catch the case when the action was brought to the fore and not created.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { finish(); return; } // Regular activity creation code... } 
0
source

Source: https://habr.com/ru/post/900367/


All Articles