I have Main Activity. That has 4 tabs(TabHost
). I have overridden onBackPress()
in MainActvity , as well As All 4 activities. This button show user a dialog box and for conformation of Exit
When app start. It show 1st tab. Then if I press back it work fine. But if I go for next 3 tab and then press back, The app stop. OnDestroy()
of Main is called. But there is not dialogue for the user.Even noting is print in log cat. That I have written in onBackPressed()
method From and of 5 activities including MainActivity
.
I have also 开发者_开发百科try onKeyDown()
for back key but result is same? Have any one experience the same? Please help me.
I come to know it was difficult to open new activity inside the previous tabs when I am using TabHost. I google it and found GroupActivity is the best solution for this problem. GroupActvity Example But GroupActivity have the same problem when open new activity in the previous tab. the back button not work properly for new activity. After search I found that was due to the focus was alwasys on parent activity. I have to make
setFocusable(true); requestFocus();
on my new activity component to gain focus.
I am now using GroupActivity for customizing Tabbar and activities.As I ma also maintaining stack of activity ids in parent activity so that I can pop out the recent activity when user press back button.
else if you are NOT going to implement Activity focus then you should maintain stack in parent and when press the back button it will initiate parent onBackPressed(); and you can call the child onBackPressed() functionality as discussed in the link.
onBackPressed() not working inside ActivityGroup
well one thing i know for sure is that you will always get the onBackPressed() called in the MainActity if you are running with a tabHost and not in the child views. The only thing that comes to mind is if you have consumed the event in the onBackPressed method (return true) because if you didnt it will go and still follow the default process and destroy your activity.
I meet this problem,but i have shot it now.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showExitDialog();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void showExitDialog()
{
new AlertDialog.Builder(this)
.setTitle("Attention")
.setMessage("Do you want to exit this application")
.setPositiveButton("YES", exitListener)
.setNegativeButton("No", cancelListener)
.show();
}
at the first time i lost a "reture true" in onkeydown()
精彩评论