I'm new to android.. I'm creating a sample app with 2 pages.. simply I want to exit my app if I press "back key" in android from any screen.. for ex: consider that I'm in page 2 and I'm pressing back key, now I want my app to exit rather than going to page 1.
Asked
Active
Viewed 144 times
5 Answers
1
FYI .
At first add finish method when calling Intent
Intent intOBJ = new Intent(ActivityA.this, ActivityB.class);
startActivity(intOBJ );
finish();
Then goto ActivityB.class
Add this onBackPressed() method
@Override
public void onBackPressed()
{
super.onBackPressed();
this.finish();
}
finish()->Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

IntelliJ Amiya
- 74,896
- 15
- 165
- 198
-
I am not agree with this. It will return to Activity 1. – Harry T. May 11 '17 at 06:24
-
@TruongHieu Kindly review my answer – IntelliJ Amiya May 11 '17 at 06:30
-
You only call the launcher and finish 2nd activity, but the first one still there. – Harry T. May 11 '17 at 06:32
-
@TruongHieu then when launching new activity you must add `finish() ;` – IntelliJ Amiya May 11 '17 at 06:37
-
where do I need to add this? in my activity 1 onBackPressed()? or in activity 2? @Amiya – Vijay May 11 '17 at 06:44
-
@Vijay at first when you call Activity 2 add `finish();` and then goto Activity 2 and write `onBackPressed` method – IntelliJ Amiya May 11 '17 at 06:49
-
I think like I'm confusing you @Amiya.. let me explain little more clearly.. I'm now in activity 2 and I want to go exit my app and I don't want this to take me to activity 1 instead.. I have tried your code but it takes me to Activity 1. – Vijay May 11 '17 at 06:55
-
nope not yet..!! @Amiya – Vijay May 11 '17 at 06:57
1
Each activity you have to override onBackPressed method. In this method you call a method name
this.finish(); or finish();

Community
- 1
- 1

faran.javed
- 418
- 2
- 15
-
-
For this you must have to start second activity like this. `Intent intent = new Intent(this , secondActivity.this); startActivity(intent); finish();` – faran.javed May 11 '17 at 06:28
0
Each activity you have to override onBackPressed
method.
Then you type
finishAffinity();
System.exit(0);

Harry T.
- 3,478
- 2
- 21
- 41
-
1
-
-
2You should not call System.exit(). It could mess up Android's handling of the lifecycles of your activities and result in an awkward user-experience (e.g. when killing the process, the previous activity from which you launched your activity may be gone as well. Android may try to restart the process again and re-create that accidentally killed parent-activity) – shinilms May 11 '17 at 06:27
-
I have tried using finish() in activity 2; but it is taking me to activity 1.. but I want my app to exit.. – Vijay May 11 '17 at 06:31
-
0
call finish();
after you start an intent from an activity.
eg:
Intent i = new Intent(ActivityA.this, ActivityB.class);
startActivity(i);
finish();

shinilms
- 1,494
- 3
- 22
- 35
-
1
-
-
-
Sorry about that. I was wrong. It will finish the previous activity after starting new one. This can be an acceptable answer. – Harry T. May 11 '17 at 06:33
-
0
@Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}

Md Mobinur Rahman
- 395
- 1
- 9