-2

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

Praful C
  • 162
  • 1
  • 3
  • 14
  • You want to keep the counter running or pause it let's say it's value is 12 and you start activity B and than return to A and it will resume from 12? – Asama May 01 '16 at 08:29
  • yeah.. it has to remain 12 only.. counter increment is done only in activity A by using button B1 – Praful C May 01 '16 at 08:38

1 Answers1

0

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.

Asama
  • 385
  • 2
  • 14
  • It's my pleasure to help you. Please mark this answer as accepted to help others find the solution :) – Asama May 01 '16 at 08:46