I have a Tab Activity and 5 child activities.
The Tab Activity is the launcher activtiy.
When Activity 1 is started using tabHost.setCurrentTabByTag(CommonParams.HOME_TAG);, the phone’s back-button-press, fires the OnBackPressed of Actvitiy 1.
But when i start Activity 1 and then navigate to Activity 2 by clicking on the 2nd Tab, and then come back to Activity 1 by clicking on 1st tab, then the phone’s back-button-press does not fire the OnBackPressed(). It makes the app to exit.
I load the activities on tab press by the following code.
intent = new Intent().setClass(this, Activity1.class);
spec = tabHost
.newTabSpec(CommonParams.HOME_TAG)
.setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Activity2.class);
spec = tabHost
.newTabSpec(CommonParams.PAGE2_TAG)
.setIndicator("Page2",
res.getDrawable(R.drawable.ic_tab_page2))
.setContent(intent);
tabHost.addTab(spec);
So when navigate from Home to Page2 and then back to Home using the tabs, Activity 1 is not started fresh. It is resumed. I suppose, OnBackPressed() is not getting fired on resumed activites.
Is it something like the activity lost its focus? If so how to avoid the activity losing focus, or how to give focus to the activity OnResume()?
Could someone help me to find why the OnBackPressed is not firing and how to make it work.
Thanks in Advance
what you are describing is the normal / default behavior of android. it’s exactly how it designed to work. if you’d like it to work different – you’ll need to implement it yourself with overriding the
onBackPressed()callback of the main tab activity.anyway – for the behavior you’d like to achieve – I’m recommending you not to use tabs and
TabActivity, butFragmentsandFragmentManager. fragments have the built in mechanism handling back button requests in the why you need, and android recommends using it anyway. that’s why theTabActivitydeprecated long time ago, since Honeycomb, and the competability back provided for Froyo and Gingerbread which adding the android fragments oriented API.this is what google wrote in the documentation of
TabActivity:google don’t want that the
TabActivitywould be use anymore.