Android kill process [duplicate]

匆匆过客 提交于 2019-11-30 18:30:42
Gunaseelan

System.exit() does not kill your app if you have more than one activity on the stack

Use android.os.Process.killProcess(android.os.Process.myPid()); this way.

for sample

public void onBackPressed() {
    android.os.Process.killProcess(android.os.Process.myPid());
    super.onBackPressed();
}

For more detail see Is quitting an application frowned upon? , How to force stop my android application programmatically?

I hope this will help you.

You can use this function to close your application

Process.killProcess( Process.myPid() ); 
  • Use startActivityForResult to start any activity.
  • Write finish() in onActivityResult of the activity in which you are starting another activity if result == RESULT_CANCELLED.
  • Finally use finish() at the point of exit.

i think you want this

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

Finish ()

Added in API level 1 Call this when your activity is done and should be closed. The Activity Result is propagated back to whoever launched you via onActivityResult().

you must use System.exit() to kill your application on a click to directly close the activity.

Why finish() is not working for you ?

Take an example where Activity A had called Activity B and when you call finish() on your activity B if finish the activity but as Activity A had given him called and it is a previous activity it returns back you to Activity A screen

What you can do?

  1. You can call System.exit() - It will directly kill the application wherever it is called from it does not matter.
  2. Use Process.killProcess(your process id);

  3. Start the home activity by killing your current activity by using finish() and then call

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