2013-09-26 65 views
1

我想在用戶按下後退按鈕時關閉彈出窗口。由於我在片段的上下文中,我沒有可用的方法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"); 
    } 

後退按鈕正常工作的應用程序一旦我按後退按鈕時,這個片段打開,但彈出沒有被駁回時,我按回到關閉按鈕...有什麼建議,我可能做錯了什麼?

感謝

+0

是什麼阻止你實現在'onBackPressed()''中的Activity'方法的解僱代碼? – Luksprog

+0

我需要調用super.onBackPressed(),因爲我需要以某種方式退出應用程序...但你能更具體請我不知道我明白:) –

+0

'popup'是什麼?對於對話框你通常有dialog.setCancelable(true);選項 – cYrixmorten

回答

0

片段被暫停(lifecycle of fragment)時,彈出窗口顯示,所以sync_popup得到空值和isThePoupShowing方法總是得到,當它從活動稱爲假值。

當您將popupWindow作爲靜態成員時,當片段暫停並且您可以正確解除它時,系統將不會「回收」成員。

+0

終於設法弄明白,謝謝你的提示 –

0

更換

popupWindow.setOutsideTouchable(false); 

與此

popupWindow.setOutsideTouchable(true); 
popupWindow.setFocusable(true);