如何完成一项活动,当你的弹出窗口充气时,请点击后退



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

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();
}
}
});
}
}

设置一个布尔值,如果您的弹出窗口打开,则该布尔值设置为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
}

最新更新