繁体   English   中英

如何完成一项活动,当你有一个膨胀的弹出窗口时

[英]How to finish an activity, onbackpressed when you have a pop up inflated

对于我当前正在构建的应用程序,如果没有互联网连接,我会在活动上弹出一个弹出窗口,问题是如果用户按下手机的后退按钮,弹出窗口就会消失,但你仍然可以在没有任何信息的情况下看到活动。 当按下后退按钮时,我也想找到一种完成活动的方法。 这是当前正在膨胀的弹出窗口的代码:

public void connection_error(final Class<?> clss){
        if(!Functions.isOnline(getApplicationContext())) {
            LayoutInflater inflater = (LayoutInflater)
                    getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = inflater.inflate(R.layout.popup_nointernet, null);

            int width = LinearLayout.LayoutParams.MATCH_PARENT;
            int height = LinearLayout.LayoutParams.MATCH_PARENT;
            boolean focusable = true; // lets taps outside the popup also dismiss it
            final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

            popupWindow.showAtLocation(findViewById(R.id.drawer_layout), Gravity.CENTER, 0, 0);

            Button btn_retry = popupView.findViewById(R.id.btn_retry);
            btn_retry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Functions.isOnline(getApplicationContext())) {
                        finish();
                        popupWindow.dismiss();
                        launchActivity(clss);
                    } else {
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.label_no_internet), Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }

如果您的弹出窗口打开,则设置一个 boolean 设置为 true。 就像是,

boolean popUpOpen = true;

覆盖 onBackPressed 方法:

@Override
public void onBackPressed() {
     if(popUpOpen){
       super.onBackPressed(); // Closes Popup.
     }
     super.onBackPressed(); // Normal Behaviour - Finishes Activity if popup is open
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM