How can I go back to the previous activity, if a certain condition is met or if a certain button is clicked?
I am using startActivity(intent)
to start this activity

- 21
- 4
-
Need more information Such as if you wish to pop activity from backstack – prashant17 Jul 17 '18 at 16:47
-
There's a difference between "parent activity" and "previous activity in backstack". Which one do you want to navigate to? – Sourabh Jul 17 '18 at 17:35
4 Answers
This answer explains it properly, https://stackoverflow.com/a/15933890/6756421
Below is the answer from the above link answered by palindrom,
You declared activity A with the standard launchMode in the Android manifest. According to the documentation, that means the following:
The system always creates a new instance of the activity in the target task and routes the intent to it.
Therefore, the system is forced to recreate activity A (i.e. calling onCreate) even if the task stack is handled correctly.
To fix this problem you need to change the manifest, adding the following attribute to the A activity declaration:
android:launchMode="singleTop"
Note: calling finish() (as suggested as solution before) works only when you are completely sure that the activity B instance you are terminating lives on top of an instance of activity A. In more complex workflows (for instance, launching activity B from a notification) this might not be the case and you have to correctly launch activity A from B.

- 130
- 1
- 9
Just use (if going to the main activity):
Intent intent = new Intent(currentActivity, homeActivity);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
whenever the condition is met or if something is clicked.

- 440
- 2
- 13
Use finish()
in your activity to close it and return to the previous

- 1,074
- 7
- 15