[Solved] Android: Return to previous activity without calling finish()

I have an android application with a LOT of activities, think of something like a book where every page is a new activity. The user can make changes in each activity, for example highlight certain texts with different colored markers etc. and it’s crucial that I’ll remember this information as long as the application stays alive (and I don’t want/need to remember any of this when it’s not). As I understand the best mechanism for storing this kind of information is via onSaveInstanceState(Bundle outState) and onCreate(Bundle)/onRestoreInstanceState(Bundle) rather than lets say, the Preferences mechanism. My only problem is that the user can navigate backwards to previous pages (Activities) and the only way i know of achieving this is by calling finish() which of course kills the current activity without calling onSaveInstanceState(Bundle outState) and even if it did call it, the next time I would launch an activity representing that page it would be an entirely new instance. So my question is: Is there a way to go back to the previous activity without calling finish()? or, is there a better way to save this information? maybe through static variables?


I know I could also implement my app differently so that not every page would have its own activity but this isn’t the answer I’m looking for.

Solution #1:

Try to use startActivity() with flags such as Intent.FLAG_ACTIVITY_CLEAR_TOP.
You could read the full story here:

Respondent: Nguyen Minh Binh

Solution #2:

I had exactly the same problem and after I thought and tried a lot, IMHO I found the most feasible solution. I inherited the Activity and adds a static method to kill if exists only. So, instead of killing the activity whenever it exits. I kill it whenever it is called again. For example,

startActivity(new Intent(this, MyActivity.class));

So that, the app will always be singleton and save its state with onSaveInstanceState.

Respondent: Ömer

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.