androidinstancestate

Android SaveInstanceState


I have two activities, say A and B. Activity A has a Text View, two buttons B1 to increment the counter and B2 to navigate to activity B. I have entered some text into Text View and incremented the counter value. I navigate to activity B by clicking button B2. Activity B2 has Up navigation that returns to activity A. I need to retain the text view and counter value when i return to activity A from B. What is the best way to do it...??? I have tried SavedInstanceState. But OnrestoreInstanceState is not called when i return to activity A from B. I have tried Shared Preferences also. But it i think its not the best way..andro


Solution

  • Add this to your activity B in your Manifest:

        <activity android:name="com.example.android.ActivityB" android:label="@string/title_activity_view_news" android:parentActivityName="com.example.android.ActivityA"> 
    <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.android.ActivityA" /> 
    </activity>
    

    And this to your activity A in manifest:

    <activity android:name="com.example.android.ActivityA" android:label="@string/app_name" android:launchMode="singleTop"> //**HERE**//  
    </activity>
    

    Change values to match your packages and activity names... So you have to add android:launchMode="singleTop" in your <activity> A section.