2

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

user1491548
  • 111
  • 1
  • 9

3 Answers3

1

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.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • But I what to pause activity1 when I pass to activity2. And when I change from activity2 to activity3 I finish activity2. But activity1 is paused on background and I want to finish it when I try to System.exit();. – user1491548 Aug 01 '12 at 12:52
  • If you close activity 1, (and 2?) Then when you click the button to close activity 3 (`finish();`), the whole application will close, as there are no longer any activities on the stack. Or see @userSeven7s answer. Actually, yes. Follow userSeven7s answer, I missed that you wanted to resume activity1 in some scenarios. – IAmGroot Aug 01 '12 at 13:13
1
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

Community
  • 1
  • 1
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
1
  • 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...
Ron
  • 24,175
  • 8
  • 56
  • 97
  • This ^^^. Starting both activities from Activity1 would give you more control over what's happening. It doesn't sound like you actually WANT the activities to stack so this would work well. – DeeV Aug 01 '12 at 13:04
  • I'm not understanding the third step! – user1491548 Aug 01 '12 at 13:16
  • Check this link ... http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html – Ron Aug 01 '12 at 13:29