我一直试图表明“你想退出吗?”用户尝试退出活动时的对话框类型。
但是我找不到合适的API挂钩。 Activity.onUserLeaveHint()
最初看起来很有希望,但我找不到阻止活动完成的方法。
答案 0 :(得分:386)
在Android 2.0+中,这看起来像:
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
在早期版本中,它看起来像:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK) {
//Ask the user if they want to quit
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.quit)
.setMessage(R.string.really_quit)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
YourClass.this.finish();
}
})
.setNegativeButton(R.string.no, null)
.show();
return true;
}
else {
return super.onKeyDown(keyCode, event);
}
}
答案 1 :(得分:175)
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ExampleActivity.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
答案 2 :(得分:32)
修改了@ user919216代码..并使其与WebView兼容
@Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
答案 3 :(得分:22)
我更喜欢在后退按钮上双击而不是退出对话框退出。
在此解决方案中,它会在第一次返回时显示吐司,警告另一次按下将关闭应用程序。在这个例子中不到4秒。
private Toast toast;
private long lastBackPressTime = 0;
@Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to close this app", 4000);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
来自:http://www.androiduipatterns.com/2011/03/back-button-behavior.html
的令牌答案 4 :(得分:20)
如果您不确定“back”的调用是否会退出应用程序,或者将用户带到另一个活动,您可以将上述答案包装在一个支票isTaskRoot()中。如果您的主活动可以多次添加到后台堆栈,或者您正在操纵后台堆栈历史记录,则会发生这种情况。
if(isTaskRoot()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourActivity.super.onBackPressed;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
super.onBackPressed();
}
答案 5 :(得分:5)
使用Lambda:
new AlertDialog.Builder(this).setMessage(getString(R.string.exit_msg))
.setTitle(getString(R.string.info))
.setPositiveButton(getString(R.string.yes), (arg0, arg1) -> {
moveTaskToBack(true);
finish();
})
.setNegativeButton(getString(R.string.no), (arg0, arg1) -> {
})
.show();
您还需要在gradle.build中设置级别语言以支持java 8:
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
答案 6 :(得分:5)
在中国,大多数应用程序将通过&#34;单击两次&#34;:
确认退出boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
答案 7 :(得分:5)
首先从super.onBackPressed();
方法移除onbackPressed()
代码而不是代码:
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
答案 8 :(得分:2)
我喜欢@GLee方法并将其与下面的片段一起使用。
@Override
public void onBackPressed() {
if(isTaskRoot()) {
new ExitDialogFragment().show(getSupportFragmentManager(), null);
} else {
super.onBackPressed();
}
}
使用片段对话:
public class ExitDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.exit_question)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getDialog().cancel();
}
})
.create();
}
}
答案 9 :(得分:2)
Just put this code in your first activity
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
drawerLayout.closeDrawer(GravityCompat.END);
}
else {
// if your using fragment then you can do this way
int fragments = getSupportFragmentManager().getBackStackEntryCount();
if (fragments == 1) {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
} else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
}
答案 10 :(得分:1)
这很好用
@Override
public void onBackPressed() {
if (mExitOnBackPress) finish();
else {
Toast.makeText(this, R.string.msg_confirm_exit, Toast.LENGTH_SHORT).show();
mExitOnBackPress = true;
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
mExitOnBackPress = false;
}
}, 2000);
}
}
答案 11 :(得分:1)
另一个替代方案是在第一个背面按下时显示Toast
/ Snackbar
,要求再次按回退出,这比显示的更少干扰AlertDialog
确认用户是否要退出该应用。
您可以使用DoubleBackPress Android Library
通过几行代码实现此目的。 Example GIF showing similar behaviour.
首先,将依赖项添加到您的应用程序:
dependencies {
implementation 'com.github.kaushikthedeveloper:double-back-press:0.0.1'
}
接下来,在您的Activity中,实现所需的行为。
// set the Toast to be shown on FirstBackPress (ToastDisplay - builtin template)
// can be replaced by custom action (new FirstBackPressAction{...})
FirstBackPressAction firstBackPressAction = new ToastDisplay().standard(this);
// set the Action on DoubleBackPress
DoubleBackPressAction doubleBackPressAction = new DoubleBackPressAction() {
@Override
public void actionCall() {
// TODO : Exit the application
finish();
System.exit(0);
}
};
// setup DoubleBackPress behaviour : close the current Activity
DoubleBackPress doubleBackPress = new DoubleBackPress()
.withDoublePressDuration(3000) // msec - wait for second back press
.withFirstBackPressAction(firstBackPressAction)
.withDoubleBackPressAction(doubleBackPressAction);
最后,将此设置为背面按下的行为。
@Override
public void onBackPressed() {
doubleBackPress.onBackPressed();
}
答案 12 :(得分:0)
如果您使用片段而不是活动,请将此代码放入您的主要活动中
private Toast toast;
private long lastBackPressTime = 0;
@SuppressLint("WrongConstant")
@Override
public void onBackPressed() {
int fragments = getFragmentManager().getBackStackEntryCount();
if (fragments == 0) {
// make layout invisible since last fragment will be removed
if (this.lastBackPressTime < System.currentTimeMillis() - 3000) {
toast = Toast.makeText(this, "Press back again to close this app", 3000);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
}else{
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}else{
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
答案 13 :(得分:0)
显示对话框以确认用户希望退出Android活动。
将以下代码添加到onBackPressed重写方法。
@Override
public void onBackPressed() {
hideViews();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit the activity?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
finishAffinity();
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
希望对您有帮助。
答案 14 :(得分:0)
如果您在应用程序中使用片段。.这是解决方法
Coach.hasOne(Team) // `coachId` will be added on Team / Target model