【问题标题】:Dismiss popup window from Fragment like onBackPressed从片段中关闭弹出窗口,如 onBackPressed
【发布时间】: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");
    }

当我在片段打开时按下后退按钮后应用程序关闭时后退按钮正常工作,但当我按下后退按钮时弹出窗口没有关闭......关于我可能做错了什么的任何建议?

谢谢

【问题讨论】:

  • 是什么阻止您在 ActivityonBackPressed() 方法中实现解除代码?
  • 我有点需要调用 super.onBackPressed() 因为我需要以某种方式退出应用程序......但你能否更具体一些,我不确定我是否理解:)
  • 什么是“弹出”?对于对话框,您通常有 dialog.setCancelable(true);选项
  • 这是一个扩展 PopupWindows 的类...

标签: android android-fragments


【解决方案1】:

显示弹出窗口时,片段会暂停 (lifecycle of fragment),因此 sync_popup 获取 null 值,而 isThePoupShowing 方法在从 Activity 调用时始终获取 false 值。

当你将popupWindow设为静态成员时,系统不会在fragment暂停时“回收”该成员,你可以正确的将其关闭。

【讨论】:

    【解决方案2】:

    替换

    popupWindow.setOutsideTouchable(false);
    

    有了这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多