I have an Android tab application. Suppose at the third tab, I exit the app. When I starts the app again, it gets restated and shows the first tab. My requirement is when aplication starts it should go to the previously selected tab. How to do this?
-
could you maybe tell us, if you're talking about "suspending" the app through the normal back button or home button. Or are you talking about killing the app? – Chris Mar 09 '11 at 10:34
-
no..i just want to suspend the application – indira Mar 09 '11 at 10:45
-
1then you can use the onSavedInstanceState mechanism stated in my answer without any problems. – Chris Mar 09 '11 at 10:47
5 Answers
You'll have to override the Method "onSavedInstanceState"
Please see this question:
Saving Android Activity state using Save Instance State
it is described there in detail.
Edit: as stated in the comments this only works with the normal way you would close your app on your phone and it is not neither persistent after reboot of the phone nor after killing your apps process.
-
2I thing the question is about exiting the app not switching between different activities – ingsaurabh Mar 09 '11 at 10:18
-
@TOX1C, from a lifecycle point of view, it's the same thing, especially since in Android, the user does not exit apps, merely switches to a different activity. – RivieraKid Mar 09 '11 at 10:24
-
@RivieraKid I dont think that's correct although android app are not supposed to be finished but the example is for scenario when activity is covered by another activity then the example will work fine what if I rebooted the device it will never work as desired I think you should read the answer of Dave L. on that same page – ingsaurabh Mar 09 '11 at 10:28
-
Okay, although the question was about "exiting" the app which is nothing but "pausing" the app in android. If you really need the TabState after rebooting or killing your app you could then store it in the shared Preferences like in milinds answer. – Chris Mar 09 '11 at 10:31
If this all you wanted then override the back button of your activity and save the value of current displaying tab using in SharedPeferences
int currentTab = mHost.getCurrentTab();
and in OnCreate after you inflate your layout get the values stored above and set that value as current tab using
TabHost mHost = new TabHost(this);
mHost.setCurrentTab(currentTab );

- 15,249
- 7
- 52
- 81
For that you need to save some tag in shared preference and when the application starts again you have to read it from shared preference and call that particular tab according to that tag saved in preference.

- 56,621
- 29
- 151
- 198
for this u can use SharedPeferences like this.
SharedPreference s readHistory = context.getSharedPreferences(className.PREFS_NAME,0);
return readHistory.getString("from", "");
set pres from all your activity.
and check when u again start your app like this.
if(frmAct.equalsIgnoreCase("activity1")) {
\\ call here your activity
}else if(frmAct.equalsIgnoreCase("activity2")) { \\ call here your activity
}

- 960
- 4
- 12
- 38
You can save the data for using in future:
1) you can save the simple data in sharedpreferences. 2) you can also save the data in database
To save the last data you have to save the data in activity's onStop() method. Because before exiting from the application the onStop() method is called of the current activity.
This link Data Storage in Android will help you a lot.

- 5,416
- 5
- 24
- 38