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"));
}
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 inonStart().Here’s some example code,
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.