I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this
Just run the below two lines when you want to exit from the application
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
You can call System.exit(); to get out of all the acivities.
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
this will clear Task(stack of activities) and begin new Task
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(1);
in the fragment
getActivity().finishAndRemoveTask();
in the Activity
finishAndRemoveTask();
Actually every one is looking for closing the application on an onclick event, wherever may be activity....
So guys friends try this code. Put this code on the onclick event
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);