1

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

4 Answers4

1

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.

heethjain21
  • 130
  • 1
  • 9
0

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.

Nigel Brown
  • 440
  • 2
  • 13
0

Use finish() in your activity to close it and return to the previous

Alexei Artsimovich
  • 1,074
  • 7
  • 15
0

You can use finish() or super.onBackPressed()

Arash
  • 825
  • 2
  • 11
  • 19