Android - restore last viewed Activity
Asked Answered
K

3

9

I have 3 different Activities that user navigates between in no particular order. My goal it twofold:

  1. When user switches to something else when app is resumed I want to start where user left even if app was terminated
  2. When last activity is resumed I want to restore it to the last viewed state (this one I think I have a pretty good idea on how to achive)

I think the problem is not start/stop - where I pretty much get what I need, but onCreate() if app was terminated. In that case - it picks Activity that I configured in the manifest. I suppose I can put something in onCreate() method of that default activity but is there a better way that I'm maybe missing?

Kislev answered 20/9, 2009 at 1:25 Comment(0)
P
6

If your app hasn't been "terminated" then #1 should already work and #2 just requires saving any values that aren't managed automagically into the Bundle in onSaveInstanceState() then restoring them in onRestoreInstanceState().

This is kind of a hack, but I think your best option for #1 in the case of the app actually being terminated would be to save the most recent Activity in the onResume of each of your Activity classes then when you first run the onCreate of your first activity do a check then start the correct Activity... maybe even put in a blank Activity at the beginning. Something like this:

StartActivity:

public class StartActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get last open Activity
        String lastActivity = PreferenceManager.getDefaultSharedPreferences(this).getString("last_activity", "");
        if (last_activity == MyActivity2.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity2.class));
        } else if (last_activity == MyActivity3.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity3.class));
        } else {
            // assume default activity
            startActivityForResult(new Intent(this, MyActivity1.class));
        }
    }

    public void onActivityResult() {
        // kill the activity if they go "back" to here
        finish();
    }
}

Then in all the other Activities (MyActivity1,2,3) save the values like so:

@Override
public void onResume() {
    Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit();
    e.putString("last_activity", getClass().getSimpleName());
    e.commit();

    super.onResume();
}

You'll also have to handle saving /restoring the data for each Activity manually. You could save all the values you need into the preferences inside the onPause() of each of the Activities then restore it in the onResume().

Presocratic answered 20/9, 2009 at 8:19 Comment(3)
Thanks fiXedd, I don't think this is a hack. It's sounds like a dispatcher pattern. I was just hoping that I'm missing something and there's easier way of doing this.Kislev
How about do startActivity and then just finish() instead of startActivityForResult?Triplett
Also pay attention to your activity launchMode setting in the manifest. I had set it to "singleTask" but changed it to "singleTop" in order to let it correctly resume the last activity. I found a nice explanation at intridea.com/blog/2011/6/16/…Sides
T
1

Keep in mind that onSaveInstanceState() isn't workable for long-term state, i.e. the user powers the phone down and then turns it back on at an indeterminate point later. You will need to create your own long-term state mechanism if you wish for your state to survive power cycles.

Teplica answered 21/9, 2009 at 15:20 Comment(0)
S
0

I believe you want to implement onSaveInstanceState and that will store your activities current state in a bundle. That bundle will be passed into your activity in onCreate, and you can use it to reset your values.

http://developer.android.com/guide/topics/fundamentals.html#actstate

Spoony answered 20/9, 2009 at 1:59 Comment(1)
That goes mostly for #2 as I understand. I need to pick the activity user was viewing last. Then I can restore its stateKislev

© 2022 - 2024 — McMap. All rights reserved.