Android 退出应用程序

发布于 2024-12-02 06:55:42 字数 827 浏览 0 评论 0原文

不建议直接退出应用程序,因此重点是完成所有活动,以便所有内容都进入后台并且用户返回主屏幕。我的问题是我有主要活动,它总是使用标志 Intent.FLAG_ACTIVITY_CLEAR_TOP | 启动。 Intent.FLAG_ACTIVITY_SINGLE_TOP 它启动另一个活动,我想在其中放置退出按钮。为了让一切都回到后台,我必须完成当前和主要活动。我虽然使用这些标志和应该退出的额外信息启动主活动可以解决问题,但是随意图传递的额外内容不会到达主活动 - 它仍然获得 android 用于启动应用程序的意图。

换句话说,我尝试过这样的事情:

// Exit's onClick:
Intent intent = new Intent(someContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("exit", true);
context.startActivity(intent);
currentActivity.finish();

// MainActivity onCreate:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
  // application never reach this point
  boolean exit = extras.getBoolean("exit");
  if (exit)
  {
    finish();
    return;
  }
}

额外的内容已交付。我怎样才能让它工作?

谢谢

it's not recommended to exit application directly, so the point is to finish all activities so everything goes to the background and user returns to the home screen. My problem is that I have main activity, which is always launched with flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP and it launches another activity, where I want to place the exit button. In order to get everything to the background I have to finish both current and main activity. I though that launching main activity with those flags and extra info that it should exit would do the trick, but the extras delivered with the intent do not reach main activity - it still gets the intent that was used by android to launch the application.

In other words, I tried something like:

// Exit's onClick:
Intent intent = new Intent(someContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("exit", true);
context.startActivity(intent);
currentActivity.finish();

// MainActivity onCreate:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
  // application never reach this point
  boolean exit = extras.getBoolean("exit");
  if (exit)
  {
    finish();
    return;
  }
}

The extras are delivered. How can I get it working?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

西瑶 2024-12-09 06:55:42

设置这 2 个标志时,如果 MainActivity 仍在堆栈中,则它不会到达 onCreate()。这将调用 onNewIntent()。

检查这 2 个链接以获取更多信息:

http://developer.android.com /reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

http://developer.android.com/reference /android/app/Activity.html#onNewIntent%28android.content.Intent%29

When setting this 2 flags MainActivity will not reach onCreate() if it is still in the stack. This would call onNewIntent().

Check this 2 links for more information:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29

旧伤还要旧人安 2024-12-09 06:55:42
System.Exit(0);

//您可以使用它来退出整个应用程序,而不仅仅是一个活动

System.Exit(0);

//You can use that to exit the entire application, not just one activity

梦巷 2024-12-09 06:55:42

AndroidManifest.xml文件中将MainActivity的Launch Mode设置为"SingleTop",如下:

android:launchMode="singleTop"

Set Launch Mode of MainActivity as "SingleTop" in AndroidManifest.xml file as follows:

android:launchMode="singleTop"
尘世孤行 2024-12-09 06:55:42

我不知道这是否是最好的解决方案,但对我来说:

在事件侦听器上:

int pid = android.os.Process.myPid(); 
android.os.Process.killProcess(pid); 

I don't know if it is the best solution, but work to me:

On Event Listener:

int pid = android.os.Process.myPid(); 
android.os.Process.killProcess(pid); 
開玄 2024-12-09 06:55:42

在您的 Activity 上调用 moveTaskToBack(true)

call moveTaskToBack(true) on your Activity

你怎么这么可爱啊 2024-12-09 06:55:42

很简单,创建一个类并放置一个静态变量和函数,如下所示:

public static boolean isExit = false;

public static boolean CheckExit() {
 return isExit;
}

public static void setisExit(boolean b)
{
isExit = b;
}

在您的主活动中放置 YourClassName.setisExit(false); 将确保 isExit 值为 false当应用程序再次启动时。

退出:

将其放入您的 OnClick 方法中:

YourClassName.setisExit(true);
finish();

并将其放入每个活动的 OnResume 方法的第一行:

if(YourClassName.CheckExit()) {
    finish();
     return super.onResume();
   }

通过在每次活动恢复时执行此操作,检查 isExit 值,如果 true 则退出。

您可以在 google play 上查看我的应用 E Player。我实现了这个方法并且工作正常

Simple, make a class and put a static variable and function like this:

public static boolean isExit = false;

public static boolean CheckExit() {
 return isExit;
}

public static void setisExit(boolean b)
{
isExit = b;
}

And in your Main Activity put YourClassName.setisExit(false); is will make sure isExit value is false when application start again.

To Exit:

Put this in your OnClick method:

YourClassName.setisExit(true);
finish();

And put this in first line of OnResume method of every activity:

if(YourClassName.CheckExit()) {
    finish();
     return super.onResume();
   }

By doing this every time a Activity resume is check the isExit value and if true then exit.

You can check my app E Player on google play. I implemented this method and its working fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文