我想在用戶按下後退按鈕時關閉彈出窗口。由於我在片段的上下文中,我沒有可用的方法onBackPressed()
。關閉彈出窗口,如onBackPressed
關閉彈出不應該是困難的,因爲我只需要調用dismiss()
方法。問題是我不知道如何檢測按下後退按鈕
我可以使用類似的片段或有任何其他方式,我可以檢測到從這個片段後按鈕的按下?
謝謝!
以後編輯=>什麼,我試圖做
在我的主要活動我實現了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");
}
後退按鈕正常工作的應用程序一旦我按後退按鈕時,這個片段打開,但彈出沒有被駁回時,我按回到關閉按鈕...有什麼建議,我可能做錯了什麼?
感謝
是什麼阻止你實現在'onBackPressed()''中的Activity'方法的解僱代碼? – Luksprog
我需要調用super.onBackPressed(),因爲我需要以某種方式退出應用程序...但你能更具體請我不知道我明白:) –
'popup'是什麼?對於對話框你通常有dialog.setCancelable(true);選項 – cYrixmorten