Jono
Jono

Reputation: 18118

Android wont save the current state of an activity

I am trying to save some values in the onSaveInstanceState(Bundle) method of my activty by following the example here: Saving Android Activity state using Save Instance State

But it doesnt seem to load it from the Oncreate(). the bundle object is always null but whenever i call another activity, it does indeed go into the onSaveInstanceState method to save my values.

Now i read that question i just posted and someone noted how they could not get it to work in an emulator? unfortunately that is all im working on. On an emulator and cant test the app on the device as i have no device available to me right now And the web services i am interacting with are in a local VM on my work machine that cannot be accesses remotely at the moment.

My question is, is it true that saved bundles dont work on emulators? I also noticed how when i do dismiss or bring up a new activity , the activity calles onPause and then onStop. when i bring back the same activty, it goes straight to onCreate?

now according to the docs here http://developer.android.com/guide/topics/fundamentals.html#actlife That is correct in terms of what the lifecycle diagram shows but if you read below that diagram on the onStop() section it says the next step it goes into is either onRestart() or onDestroy()? no mention of onCreate? Type on the docs?

Anyways here is my onSaveInstanceState() and onCreate() both in the same activity:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    Log.d(TAG, "SAVING OauthManager in onSaveInstanceState");
    // TODO: if works, we need to save the my network list HashMap as well
    // so we dont make a call to the platform everytime we refresh this
    // screen
    // savedInstanceState.putSerializable("oauthManager", mOathManager);
    // Log.d(TAG, "finished saving");
    // super.onSaveInstanceState(savedInstanceState);
    // Log.d(TAG, "super.onSaveInstanceState(savedInstanceState)");

    savedInstanceState.putString(USER_CONSUMER_ID,
            mSavedUserConsumerTokenId);
    savedInstanceState.putString(USER_CONSUMER_SECRET,
            mSavedUserConsumerSecret);

    savedInstanceState.putString(URL_REQUEST_TOKEN, mSavedRequestTokenUrl);
    savedInstanceState.putString(URL_ACCESS_TOKEN, mSavedAccessTokenUrl);
    savedInstanceState.putString(URL_AUTHORIZE_TOKEN,
            mSavedAuthorizeTokenUrl);
    super.onSaveInstanceState(savedInstanceState);
}

My onCreate

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.d(TAG, "    ");
        Log.d(TAG, "onCreate");
        Log.d(TAG, "///////////////////////////////////////");
        Log.d(TAG, "///////////////////////////////////////");
        super.onCreate(savedInstanceState);
        Log.d(TAG, "super.onCreate(savedInstanceState);");
        setContentView(R.layout.network_list);
        mContext = getApplicationContext();
        mIntent = getIntent();


        mGoogleButton = (Button) findViewById(R.id.googleAddOrRemoveButton);
        mFacebookButton = (Button) findViewById(R.id.facebookAddOrRemoveButton);
        mLinkedInkButton = (Button) findViewById(R.id.linkedInAddOrRemoveButton);
        mPopEmailButton = (Button) findViewById(R.id.popEmailAddOrRemoveButton);


        if (savedInstanceState != null) {
            Log.d(TAG, "inside if (savedInstanceState != null) {");
            Log.d(TAG, "savedInstanceState != null");
            // mOathManager = (OAuthManager) savedInstanceState
            // .getSerializable("oauthManager");
            mSavedUserConsumerTokenId = savedInstanceState
                    .getString(USER_CONSUMER_ID);
            mSavedUserConsumerSecret = savedInstanceState
                    .getString(USER_CONSUMER_SECRET);

            mSavedRequestTokenUrl = savedInstanceState
                    .getString(URL_REQUEST_TOKEN);
            mSavedAccessTokenUrl = savedInstanceState
                    .getString(URL_ACCESS_TOKEN);
            mSavedAuthorizeTokenUrl = savedInstanceState
                    .getString(URL_AUTHORIZE_TOKEN);

            mOathManager = new OAuthManager(mContext, getIntent(),
                    mSavedUserConsumerTokenId, mSavedUserConsumerSecret,
                    mSavedRequestTokenUrl, mSavedAccessTokenUrl,
                    mSavedAuthorizeTokenUrl, CALLBACK_URI);

            mOathManager.requestUserRequestToken();
        } else{

            Log.d(TAG, "savedInstanceState is null and loading the list again");
            GetNetworkListTask getNetworkListTask = new GetNetworkListTask();
            getNetworkListTask.execute();
        }




    }

Upvotes: 1

Views: 2911

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

the bundle object is always null but whenever i dismiss the activty it does indeed go into the onSaveInstanceState method to save my values.

If by "dismiss" you mean "press the BACK button", then the Bundle from onSaveInstanceState() (if any) is discarded, as the user has indicated they are finished with the activity. The onSaveInstanceState() Bundle is used in cases where the user has not said they are finished with the activity (e.g., they accepted an incoming phone call) yet Android elects to destroy the activity to free up RAM.

My question is, is it true that saved bundles dont work on emulators?

onSaveInstanceState() works perfectly fine in an emulator. The simplest way to test onSaveInstanceState() in an emulator would be to change screen orientation ([Ctrl]-[F11]).

I also noticed how when i do dismiss or bring up a new activity , the activity calles onPause and then onStop. when i bring back the same activty, it goes straight to onCreate?

You have some fundamental misunderstandings about Android, well beyond the scope of a single StackOverflow answer.

but if you read below that diagram on the onStop() section it says the next step it goes into is either onRestart() or onDestroy()? no mention of onCreate? Type on the docs?

The diagram is generally correct. There are scenarios in which onDestroy() is not called (e.g., emergency RAM reclamation forcing your process to be killed), but that is uncommon.

Upvotes: 2

Related Questions