开发者

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百科go to the previously selected tab. How to do this?


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.


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


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.


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜