Clear stack back when application object is killed by system

I have an application with a lot of action. Among them is InitialActivity , which loads the main data. This is launch activity, and none of the following actions can work without downloaded data.

So, I launch the application, InitialActivity loads the data, then goes to MainActivity , and then browses through some FarAwayActivity . Then I move on to other applications (some of them are heavy) and my application gets killed in the background. I injected a log call into my Application.onCreate() and I see that it is called again when I return to my application. Therefore, I am sure that the application was killed in the background. But Android brings the most recent FarAwayActivity to the beginning, and it is immediately reset because all the downloaded data is zeros.

So the question is: how can I tell an application to start with InitialActivity when (and only when) it is killed and restored? Something like β€œforget my back stack,” but presumably automatically, in the manifest.

I already read and tried everything: An action tag document , but nothing matches my expectations. The closest thing is android:clearTaskOnLaunch="true" , but it clears all other actions on the stack when the application restarts from the desktop (and the application has not yet been killed, so all data is available). To be clear, I want to start with InitialActivity only after the application has actually been killed.

+6
source share
1 answer

First, I suggest you, instead of storing it in the application, you can transfer your data to your activities through Intent. Thus, when the action is restored, the saved Intent will return its data.

Now, to offer your real question:

  • When your action is restored, you will make sure that your data in the Application object is null
  • if so, you should call it:

     Intent newIntent = new Intent(FarAwayActivity.this, InitialActivity.class); newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(newIntent); 

Now it should clear the stack before looking for InitialActivity.

0
source

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


All Articles