I have a program with several activities. One can start activity B from clicking on a button in Activity A, and can start C from clicking on a button in B, and so on. In short A->B->C->D->E.
If i click the button "Exit" in Activity E, i want to shut down my whole program, in another word, i want to finish all of the 开发者_Python百科activities. How can i do it?
Thanks a lot!
i want to shut down my whole program, in another word, i want to finish all of the activities. How can i do it?
You don't. Let the user press the HOME button to return to the home screen.
I disagree with the answer above. If you have an exit option in your menu, then ideally you need the following, as not every user will think of pressing the home key (basically, each activity detects that exit has been chosen, and then sends a result telling the next activity to close and then passes the result on again, and again, until they're all closed:-
Use the code below to call each new intent (e.g. when A starts B. Note the startActivityForResult).
Intent mainIntent = new Intent(FromThisClassName.this,NewClass.class);
startActivityForResult(mainIntent, 2); // change the number for each activity/intent
Then, for each intent, add the following code:-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("CHECK","RequestCode = "+requestCode+" ResultCode = "+resultCode+" Intent Data = "+data);
if (requestCode == 2){
if(resultCode == 5){ // 5 = our exit all code.
this.setResult(5);
finish(); // Exit press detected. Quit now.
}
}
精彩评论