Search code examples
javaandroidapplication-lifecycleonresume

Android App/Activity To Start Fresh Completely every time it starts or resumes?


I have a kid's app for Android and there are some unique considerations for this application since the app has basically no navigation (it's for young kids). I do not want to break my app UI (which has been successful on iPhone) by adding a quit/restart button.

What I really need is fairly simple -- I want my activity/app to start clean and new every single time it starts. Whether it's an initial load or whatever -- basically any time onResume is called I want a completely fresh instance of my app.

I initially thought I could just exit/quit/finish the app when the user leaves. But I haven't found a way to do this that doesn't cause crashes on start. Also every thread/stack overflow post about that idea is filled with people wagging their fingers and saying you should never ever quit an app on android.

If I can't quit the app onExit, is there something I can do to restart my activity every time onResume is called? (or would that be an infinite loop?).

Would greatly appreciate any help!


Solution

  • Try starting your main activity in onResume, and clearing the activity stack:

    public void onResume() {
        super.onResume();
        startActivity(new Intent(this, MainScreen.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }
    

    maybe these aren't the correctl flags to add, but check out the other Intent flags and this could do what you want!

    intent flags documentation