-1

I have two different activities. The first launches the second one. I want to close the app from second Activity but using finish() the first activity is automatically displayed instead of closing application .How can I avoid this?

3 Answers3

0

As @user3676184 mentioned, you can finish first activity ones you launch second activity. i.e.

Implement this in first activity from where you are launched your second activity.

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
startActivity(intent);
finish(); //this will finish ActivityOne and only ActivityTwo will be present and when you call finish in ActivityTwo it will close.
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
0

I want to close the app from second Activity but using finish() the first activity is automatically displayed instead of closing application .How can I avoid this?

Because FirstActivity is added in back-stack and pop-up back when pressing back button on SecondActivity.

To get required behavior, after starting SecondActivity call finish() method in FirstActivity to remove from stack :

Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
FirstActivity.this.finish(); //<< close FirstActivity here
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

when you start second activity from first activity then call method finish() like,

Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
finish();
Shashank Gupta
  • 165
  • 2
  • 16