7

Android kills the application's process when it is in the background and it needs more memory.

I've read a few articles about this. Some people recommend restarting the app when this happens. But none of the articles give me information on how to do something like that.

Is there a way to go back to the root activity after an application's process has been destroyed and the app goes back into the foreground? What would be the best way to do something like this?

DennisVA
  • 2,068
  • 1
  • 25
  • 35
  • "Is there a way to go back to the root activity after an application's process has been destroyed and the app goes back into the foreground" - doesn't it happen by default when you're opening killed app? – Anton Malyshev Apr 25 '18 at 11:34
  • nope, it restores the last activity that the user was in. But without any state – DennisVA Apr 25 '18 at 11:37
  • You can check in the `onCreate` method is activity was started by you or by system (saving some parameters into `Bundle`), if by system - terminate and launch the root activity. – Anton Malyshev Apr 25 '18 at 11:41
  • that doesn't seem like a good solution, because what if we want to use the bundle for saving things on configuration changes – DennisVA Apr 25 '18 at 11:43
  • Why not to use the bundle for both purposes? – Anton Malyshev Apr 25 '18 at 11:44
  • Sorry i see this as bad practise, there has to be a better solution – DennisVA Apr 25 '18 at 11:46
  • also the bundle would not be null after rotation change, same case, so its not possible that way. We don't want to restart after a rotation change, but only when process has been killed – DennisVA Apr 25 '18 at 11:48
  • You wish to return to root activity but maintain app state or return to root activity on fresh create? – GordonW Apr 25 '18 at 11:53
  • i want to do a fresh create – DennisVA Apr 25 '18 at 11:55
  • @PrisonMike How about setting `android:noHistory="true"` for all the activities except Root activity? Drawback is whenever user puts app in background it will not retain state for that activity. – Sagar Apr 25 '18 at 11:57
  • nope that doesn't seem like an option for me, thx tho – DennisVA Apr 25 '18 at 11:58
  • what I have done in past as a solution is create a static boolean and set it true at app start, and within my onResume always check the status of this boolean, if it goes false (garbage collection) finish() the activity (or execute your recovery code). – GordonW Apr 25 '18 at 12:00
  • If i do this in the Application class, what will happen when i do startActivity(rootactivity) or something. Android will still try to restore the last activity right? that would cause some issues. I like your idea tho. I don't like the idea of letting android restart the activity and after that check if we need to go to the root activity – DennisVA Apr 25 '18 at 12:03
  • theres a few things you can do in your recovery i.e. you could use FLAG_ACTIVITY_CLEAR_TOP which would flush the entire stack and return you to root, theres a bunch of things you can do to ensure a clean return to root. – GordonW Apr 25 '18 at 12:08
  • you gave me something to think about/work with, thx – DennisVA Apr 25 '18 at 12:14

2 Answers2

6

The only solution i found that works for me is putting this code in a base class for activities to inherit:

private static boolean isFirstOnCreate = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResource());

    if (isFirstOnCreate && savedInstanceState != null) {
        startActivity(getPackageManager().getLaunchIntentForPackage(getPackageName()));
        finishAffinity();
    }
    isFirstOnCreateInvocation = false;
DennisVA
  • 2,068
  • 1
  • 25
  • 35
3

See my answer to this similar question or this answer to a similar question.

Basically you want to set a static variable when your app is started, and in each Activity you need to check if that variable is still set. If it isn't, it means that Android has killed the OS process hosting your app and created a new one after the user returns to your app. You can detect this situation and then do whatever is appropriate. Usually, this means redirecting the user to the first Activity and reinitializing your application.

David Wasser
  • 93,459
  • 16
  • 209
  • 274