indira
indira

Reputation: 6687

How to retain the previous data even if we exit the Android app?

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?

Upvotes: 2

Views: 5693

Answers (5)

Chris
Chris

Reputation: 7855

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.

Upvotes: 3

Bishnu Pada Chanda
Bishnu Pada Chanda

Reputation: 5416

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.

Upvotes: 1

milind
milind

Reputation: 970

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
}

Upvotes: 1

Chirag
Chirag

Reputation: 56925

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.

Upvotes: 1

ingsaurabh
ingsaurabh

Reputation: 15269

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 );

Upvotes: 1

Related Questions