我在做什么 ::
我正在尝试检查是否退出按下后退键上的应用程序的条件
发生了什么 ::
我可以启动对话框,但在我可以在对话框
中进行任何选择之前,立即删除了活动我该如何解决这个问题?
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
/*FragmentManager.BackStackEntry backEntry=getSupportFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount()-1);
String str=backEntry.getName();
Log.d("Back-Name", str);*/
if(getSupportFragmentManager().findFragmentByTag("HomeFragment")!=null ){
Log.d("My-Log-Msg","$-before-showDialog-$");
showGpsDialog();
Log.d("My-Log-Msg","$-after-showDialog-$");
}else
{
/*Intent intent = getIntent();
finish();
startActivity(intent);*/
ft.replace(R.id.content_frame, fragment1,"HomeFragment");
}
}
return super.onKeyDown(keyCode, event);
}
public void showGpsDialog(){
//GPS-Dialog
WillYouQuitAppDialog quitAlert;
try {
quitAlert = new WillYouQuitAppDialog();
quitAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("My-Log-Msg",e.toString());
e.printStackTrace();
}
}
答案 0 :(得分:1)
使用以下代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
或使用onBackPressed
:
@Override
public void onBackPressed() {
// code of condition or anything else that you want
}