【发布时间】:2013-09-26 11:10:22
【问题描述】:
我想在用户按下后退按钮时关闭弹出窗口。由于我在 Fragment 的上下文中,我没有可用的方法 onBackPressed()。
关闭弹出窗口应该不难,因为我只需要调用dismiss() 方法。问题是我不知道如何检测返回按钮的按下
我可以对 Fragment 使用类似的东西吗?或者有没有其他方法可以检测到从这个 Fragment 按下返回按钮?
谢谢!
稍后编辑 => 我想做什么
在我的主要活动中,我实现了这样的onBackPressed() 方法:
@Override
public void onBackPressed() {
//isThePopupShowing() is a method in the target fragment which returns true if the PopupWindow is currently showing
if (secondFragment.isThePoupShowing()) {
// dismissPopup is a method in the same fragment which closes the PopupWindow with the dismiss() method
secondFragment.dismissPopup();
Log.d("DismissPopup", "And finally here!");
} else {
super.onBackPressed();
}
}
当我在这里创建片段时:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home);
SharedPreferences user_details = getSharedPreferences(
ro.gebs.captoom.utils.Constants.PREFS_NAME, 0);
LoginFragment firstFragment = new LoginFragment();
secondFragment = new HomeScreenFragment();
String userid = user_details.getString("userid", null);
manager = getSupportFragmentManager();
if (userid == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
} else {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, secondFragment).commit();
}
}
这是我的片段中的代码:
public boolean isThePoupShowing() {
return sync_popup != null && sync_popup.isShowing();
}
//
public void dismissPopup() {
Log.d("DismissPopup", "I got here, dismissing");
sync_popup.dismissPopup();
}
这是解除方法:
public void dismissPopup(){
layout.setVisibility(View.GONE);
dismiss();
Log.d("DismissPopup", "and in SyncQuickAction");
}
当我在片段打开时按下后退按钮后应用程序关闭时后退按钮正常工作,但当我按下后退按钮时弹出窗口没有关闭......关于我可能做错了什么的任何建议?
谢谢
【问题讨论】:
-
是什么阻止您在
Activity的onBackPressed()方法中实现解除代码? -
我有点需要调用 super.onBackPressed() 因为我需要以某种方式退出应用程序......但你能否更具体一些,我不确定我是否理解:)
-
什么是“弹出”?对于对话框,您通常有 dialog.setCancelable(true);选项
-
这是一个扩展 PopupWindows 的类...