Reputation: 516
Hi, I am creating an app with tabactivity. But for security reasons i made changes and main screen will ask for password whenever the app is moved to background and started again or whenever the app is open, for that when an activity calls stop()
then I am finishing the app by calling finish()
and it is working. But the problem is I am not able to see the last tab which the user was viewing. I have used onsaveinstancestate()
which is not working in this case. What is wrong with what I'm doing here. Is there any other approach for this? any suggestions are welcome
this is my stop method
public void onStop()
{
super.onStop();
finish();
System.out.println("In the onStop() event");
}
this is my code in tabactivity
protected void onSaveInstanceState (Bundle outState){
outState.putInt("name",tabHost.getCurrentTab());
super.onSaveInstanceState(outState);}
protected void onRestoreInstanceState(Bundle outState){
super.onRestoreInstanceState(outState);
tabHost.setCurrentTab(outState.getInt("name"));
}
Upvotes: 0
Views: 180
Reputation: 22637
You need to save / restore the current tab yourself. You can do this using TabHost.set/getCurrentTab() and .get/setCurrentTabByTag(). The latter allows you to assign a string tag to the tab as opposed to dealing with the tabs by integer value.
You can simply save the selected tab into shared preferences in onStop()
, and restore it in onStart()
.
Here's some example code,
http://code.google.com/p/csdroid/source/browse/trunk/src/org/jtb/csdroid/TabWidgetActivity.java
Note that this restores the tab in onCreate()
, and saves the current tab to prefs every time the tab is changed. This is not necessarily the best example, but it gives you an idea.
Upvotes: 1
Reputation: 14058
Use a shared preference to save the selected tab of your tabhost in onStop(), and restore it in onStart().
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Editor ed =pref.edit();
ed.putInt("selected_tab",t.getCurrentTab());
ed.commit();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
t.setCurrentTab(pref.getInt("selected_tab", -1));
}
Replace t with the instance variable reference to your tabhost.
Upvotes: 0