Why Back button is not detecting in muti tab activities?

心不动则不痛 提交于 2019-12-11 07:18:27

问题


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.


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/6368012/why-back-button-is-not-detecting-in-muti-tab-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!