我正在使用Android中的许多活动。 我需要的是,在退出应用程序时,它应该弹出确认退出或不退出。 我知道只在java中创建一个不使用任何.xml文件的弹出窗口。 我也知道如何在处理只有一个活动或单击事件时执行以下操作。 但是在多个活动的情况下,通过单击后退按钮,它不起作用。
I'm working with many activities in Android. What I need is, while exiting the app, it should pop up a confirmation to exit or not. I know to create a popup window without using any .xml file only in java. I also know how to do the following thing while working with only one activity or from a click event. But in case of multiple activities and by clicking back button, it is not working.
最满意答案
首先,你应该只要求用户退出你的主(启动器活动),因为后退按钮应该总是让用户回到以前的活动,如果有的话。 如果你想这样做,你应该重载活动的onBackPressed()方法。 如果要在所有活动中提供选项,或者只想在启动器活动中提供启动器活动,则必须对所有活动实施onBackPressed()。 这里是例子。
@Override public void onBackPressed() { AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this); ab.setTitle("myDialog"); ab.setMessage("are you sure to exit?"); ab.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //if you want to kill app . from other then your main avtivity.(Launcher) android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); //if you want to finish just current activity yourActivity.this.finish(); } }); ab.setNegativeButton("no", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); ab.show(); }其他的解决方案(但有点重):在你的父母活动的一次恢复。
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); if (getIntent().getBooleanExtra("closeornot", false)) { finish(); } //after this you can write any other code. }并在你的其他(儿童活动)使用onBackPressed为:
@Override public void onBackPressed() { Intent mIntent = new Intent(yourActivity.this, Home.class); mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mIntent.putExtra("closeotnot", true); startActivity(mIntent); }First of all you should only ask user about exiting on your Main (Launcher Activity) because back button should always get user back to previous activity if any. And if you want to do this you should override activity's onBackPressed() method. You have to implement onBackPressed() on all your activities if you want to give options in all activities or just in launcher activity if you want to give on launcher activity. here is example .
@Override public void onBackPressed() { AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this); ab.setTitle("myDialog"); ab.setMessage("are you sure to exit?"); ab.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //if you want to kill app . from other then your main avtivity.(Launcher) android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); //if you want to finish just current activity yourActivity.this.finish(); } }); ab.setNegativeButton("no", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); ab.show(); }other Solution (But a Bit heavy): in your parent activity's oncereate.
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); if (getIntent().getBooleanExtra("closeornot", false)) { finish(); } //after this you can write any other code. }And in your other (Child Activities) use onBackPressed as this:
@Override public void onBackPressed() { Intent mIntent = new Intent(yourActivity.this, Home.class); mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mIntent.putExtra("closeotnot", true); startActivity(mIntent); }更多推荐
发布评论