I'm a beginner in android app development. I've developed an app which has 5 Activities: (i.e. Activity - MainActivity, B, C, D, E
).
when the user opens the app the MainActivity
will firedm and takes inputs from the user. Upon clicking a button, the user will taken to Activity B
.
There are 3 buttons in Activity B
which takes user to 3 different activities:
(i.e. button-1 --> Activity C
, button-2 --> Activity D
, Button-3 --> Activity E
).
after going into any of these 3 Activities (i.e. C
,D
,E
), on pressing back button, the user will be taken back to Activity B. and whichever Activity among C
,D
,E
the user came from will be finished.
If any Alert dialog is displayed in these 3 Activities, upon clicking on "OK"
in the dialog, the user will be taken back directly to MainActivity
to take fresh inputs from the user. and the cycle continues again.and also if the user pressed back button when Activity B
is active, then the user will be taken to MainActivity
.
when the user press back button in MainActivity
, a dialog box appears asking if the user wants to exit from the app. upon clicking on "Yes" the app should finish all the activities and close the app.
but what happening is when the user click on "yes"
the MainActivity is finishing but the Activity B
is appearing on top again. as it is in the stack, and pressing back button when on Activity B
will take user to MainActivity
. a number of times the user navigates back n forth through Activity B
, that many times the Activity B is coming up when the User clicks on "Yes"
in the dialog box.
I don't know how to close the app by finishing the activities without affecting the navigation between the activities as there is also a flow of data among those activities (mainly Activity B
). I've tried using Intent.FLAG_ACTIVITY_CLEAR_TASK
, Intent.FLAG_ACTIVITY_CLEAR_TOP
but it didn't work. The Activity B is still coming up.
Please help me resolve this problem without interrupting the navigation or data flow.
thanks in advance.
EDIT:
@Override public void onBackPressed() {
new AlertDialog.Builder(this).setMessage("Are you sure, you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
If you want to clear all activities you can use
finishAffinity()
if you are targeting 16 and 16+. For below 16 flagsCan be used. The
finishAffinity()
will finish all the activity no matter how many are there below in the task stack. if you don't want to have anActivity
visible if you comeback from anotherActivity
you can finish it when you start the another.When you click
OK
in Activity C,D,E which is meant to take you to themain activity
while skippingActivity B
, then you should specify flagI think that should work.
If that actually doesn't work (I'd be surprised), a possible alternative is using
startActivityForResult()
in ActivityB
, to which you'd write a result handlingAnd in Activity C, when you click OK button;
you may use:
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP
. (For Example)To clear all previous activitys(s) and start a new one.
and check if
exit=true
finish()
the current one.you can also use:
android:noHistory="true"
in your activity Tag in the Manifest.xmlNote: this will keep no history it means that after you open another activity the previous one will be killed.