Android back button new intent or finish()

1.1k Views Asked by At

I have a question. I have a list view (A) and a details view (B).

On B, I have a "view list" button that will always take the user to the list. The thing is, I can get to B via a notification and not necessarily from A.

So when clicking B, I can't just use finish() because I could have come in from a notification, so I may need to start a new activity in some cases.

How do I reliably tell if I came in from A or from a notification so that I can take the right action.

Added to that, is this something I should be worrying about? Or is it ok just to start an activity every time. In the case where I bouncing between A -> B -> A -> B -> A -> B over and over... never once calling finish()... will that slow things down?

Thanks,

Gerard.

2

There are 2 best solutions below

2
On BEST ANSWER

When you start Activity B with an intent add an extra and then check the state of extra in activity B. Here is an example using strings although you could use other variable types like a boolean or an int:

Intent i = new Intent(this, ActivityB.class)
i.putExtra("startedBy", "ActivityA"); 

Then in Activity B you can get the extra variable with:

Bundle extras = this.getIntent().getExtras(); 
String startedBy = null;

if (extras != null) {
    startedBy = extras.getString("startedBy");
}
0
On

You can add an intent extra flag when you go from A -> B, say a boolean flag. On Activity B, you can default the flag to false if it doesn't carry the intent extra.