Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 509
peter Avatar asked Nov 18 '10 21:11

peter


People also ask

How do I start the same activity again on Android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

How do you start an activity only once the app is opened for the first time?

Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.

What is Android activity lifecycle?

Activity Lifecycle: Activity is one of the building blocks of Android OS. In simple words Activity is a screen that user interact with. Every Activity in android has lifecycle like created, started, resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle.


2 Answers

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

like image 85
james Avatar answered Sep 30 '22 14:09

james


Ended up getting it to work fine by calling finish() in onPause().

Again, I appreciate the advice from people saying "this is not how Android does things". I've got a pretty good understanding of the best practices at this point. This is an unusual situation for an unusual type of user.

like image 41
peter Avatar answered Sep 30 '22 15:09

peter