简体   繁体   中英

After pressing home button saving the state of Activity

I have two activities, Activity1 and Activity2 . Activity1 is launcher activity which starts at the beginning. Now when I'm pressing Home button from activity2 and again opening the app from all apps then Activity1 is opening. I want to open the same activity that was opened at the time of pressing the home button. I need to save state of activity2 , but how I don't have any clue.

I looked at this and this but still I didn't got the clear picture on how to do this. Please help me as I'm new to android.

When you pressed "home" button, your activity goes to onPause() .

So I personally recommend you to override the onPause() method which can not only handle the "home" button pressed but also other circumstances.

In your case, it is only onPause() and onResume() related, so you can try put the state into SharedPreferences or Internal/External storage.

say:

in your onPause() method, do something like:

// Use Shared Preferences to save data
SharedPreferences previewSizePref = getSharedPreferences("PREF",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = previewSizePref.edit();
prefEditor.putInt("x", somethingA);
prefEditor.putInt("y", somethingB);
prefEditor.commit();

and in your onResume() , retrieve the saved data like:

SharedPreferences previewSizePref = getSharedPreferences("PREF",MODE_PRIVATE);
if (previewSizePref.contains("x") && previewSizePref.contains("y")) {
    //your saved data exists, do something
} else {
    // handle the circumstances that the saved data doesn't exist
}

For saving state in onPause() and restore in onResume() you can have a look at this answer:

Saving Activity State in the onPause

And for SharedPreferences, you can have a look at API document: Data Storage - Shared Preferences

What you're describing should be standard for behaviour for Android, and if it isn't doing what you describe then you've probably overwritten this behaviour somewhere else, probably in your manifest.

If you create a new Android application and make a simple two page app, with Activity A as the launcher and Activity B as the second page, then your manifest will look like the following and the app will display the behaviour you describe.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ActivityA"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ActivityB"
        android:label="@string/title_activity_main" >
    </activity>
</application>

Double check that your manifest looks similar to this- Activity B has no flags and is just a declaration that it exists in your manifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM