在处理多项活动时如何在android中退出应用程序时创建一个弹出窗口进行确认[关闭](How to create a popup window for confirmation while exiti

编程入门 行业动态 更新时间:2024-10-24 22:28:28
在处理多项活动时如何在android中退出应用程序时创建一个弹出窗口进行确认[关闭](How to create a popup window for confirmation while exiting an app in android when working wth multiple activities [closed])

我正在使用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); }

更多推荐

本文发布于:2023-08-05 00:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多项   创建一个   应用程序   弹出窗口   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!