views:

1658

answers:

3

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?

A: 

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

Whaledawg
That goes mostly for #2 as I understand. I need to pick the activity user was viewing last. Then I can restore its state
DroidIn.net
+1  A: 

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().

fiXedd
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.
DroidIn.net
A: 

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.

MattC