user1491548
user1491548

Reputation: 111

Cannot close android activity

I have an application with an activity (activity1) that pauses when pressing a button, opening a new activity (activity2) that has two buttons: a menu button and a button to resume the activity1. Pressing the menu button that opens a new activity (activity3) that has a button to get out of the application but instead of closing, it goes back to activity1, restarting it. How can I close the aplication or close the activity1 inside the activity 2 or 3?

Sorry my English. thank you very much

Upvotes: 2

Views: 470

Answers (3)

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

Pressing the menu button that opens a new activity (activity3) that has a   
button to get out of the application but instead of closing,   
it goes back to activity1,restarting it.  

Closing a Activity from other activity?? Not Possible.
But you can achieve it(manually) by setting a Global Variable..

Like when you press Exit Button in Activity-3,set a Global variable(int),say "int exit" and call finish() in Activity 3,this will land you in Activity-2..Now in the OnResume Method of the Activity-2 and Activity-1,Go On to Check the value of the Global Variable,if exit== 1,call finish() there.else do nothing.

Edit

Global Variable Example

Upvotes: 1

Ron
Ron

Reputation: 24233

  • Use startactivityforResult to start your second activity.
  • In second activity, when you press exit menu button, finish the activity with result code 1..
  • In onActivityResult of first activity, check the result code.. if its exit code(1) then call finish...

Upvotes: 1

IAmGroot
IAmGroot

Reputation: 13855

When you call your activity, call finish(); straight after:

Intent intent = new Intent(getApplicationContext(),MyActivity.class);
startActivity(intent);
finish();

This will run your new activity, and close the old one in the process.

Upvotes: 1

Related Questions