Android exit application

纵饮孤独 提交于 2019-12-11 06:58:30

问题


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


回答1:


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




回答2:


System.Exit(0);

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




回答3:


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

android:launchMode="singleTop"



回答4:


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



回答5:


call moveTaskToBack(true) on your Activity




回答6:


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



来源:https://stackoverflow.com/questions/7255509/android-exit-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!