当用户按下后退按钮时,我想关闭一个弹出窗口。由于我在片段的上下文中,我没有方法onBackPressed()
可用。
解雇弹出窗口并不困难,因为我只需要调用dismiss()
方法。问题是我不知道如何检测按下后退按钮
我可以使用类似的片段或者是否有其他方法可以检测到从此片段按下“后退”按钮?
谢谢!
LATER EDIT =>我想做什么
在我的主要活动中,我实现了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");
}
当片段打开时,当按下后退按钮时,后退按钮正常工作,但当我按下后退按钮时弹出窗口没有被解除...有关我可能做错的任何建议?< / p>
由于
答案 0 :(得分:0)
当弹出窗口显示时,片段暂停(lifecycle of fragment),因此sync_popup获取空值,isThePoupShowing方法在从活动中调用时始终获取false值。
当您将popupWindow作为静态成员时,系统将在片段暂停时不会“回收”该成员,并且您可以正确地将其解除。
答案 1 :(得分:0)
替换
Map
用这个
UUID