Lawrencce Clarke
Lawrencce Clarke

Reputation: 57

Check if an Activity was triggered by a particular Activity

Is it possible to check that an Activity was triggered by a particular Activity.

For example, I have 2 Activities, Activity A and Activity B and A triggered B using an Intent. And now that I am in Activity B, I want to check that Activity B was triggered by Activity A, like:

if(Activity B was trigger by Activity A) { //do something }

Upvotes: 0

Views: 356

Answers (2)

Spidy
Spidy

Reputation: 40002

You can use

getCallingActivity();

to get the name of the activity that started the current activity. But you should consider why you need this functionality. Most activities should be loose coupled.

Upvotes: 2

AlbeyAmakiir
AlbeyAmakiir

Reputation: 2247

When creating the Intent, you can pass some information, such as:

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("com.mypackage.triggeredby", "ActivityA");

And in ActivityB's onCreate or onResume, you then say:

Intent data = getIntent();
triggeredBy = data.getStringExtra("com.mypackage.triggeredby");

I like Strings, but you could alternately pass the Object, or an int, or anything you can use to identify it with.

Upvotes: 1

Related Questions