49

I have 2 activities: Activity1 and Activity2. In each of this activities there is a button that leads me to a third activity (MainActivity). In MainActivity I want to know from which activity page was called.

david.schreiber
  • 3,851
  • 2
  • 28
  • 46
tinti
  • 1,455
  • 8
  • 23
  • 39

5 Answers5

55

You can use the putExtra attribute of the Intent to pass the name of the Activity.

Calling Activity,

Intent intent = new Intent(this, Next.class);
intent.putExtra("activity","first");
startActivity(intent);

Next Activity,

Intent intent = getIntent();
String activity = intent.getStringExtra("activity");

Now in the string activity you will get the name from which Activity it has came.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 1
    If the calling activity is not from the same app ? – William Jun 22 '19 at 15:24
  • But if you refactor the name of the activity you may miss that. I would prefer something built in. Calling a method from android's API. Something like "getCallingActivity()". – mayid May 01 '20 at 15:04
23

You can use:

public ComponentName getCallingActivity()

to know which Activity called your current Activity.

brianestey
  • 8,202
  • 5
  • 33
  • 48
6

Use putExtra() to identify the previous activity.

Intent i = new Intent(Activity1.this, MainActivity.class).putExtra("from", "activity1");
startActivity(i);

To check the activity in Main Activity,

if(getIntent().getStringExtra("from").equals("activity1")){
//From Activity 1
}else {
// Activity 2
}
Chrishan
  • 4,076
  • 7
  • 48
  • 67
0

when you start your activity :

Intent intent = new Intent(activity, HistoryDetailsResults.class);
 intent.putExtra(Activity.ACTIVITY_SERVICE, activity.getLocalClassName());
 activity.startActivity(intent);

Using Activity.ACTIVITY_SERVICE I assume you use a good practice. (Activity.ACTIVITY_SERVICE is the String : 'activity')

activity.getLocalClassName() give this : view.YourActivity as String

EDIT : Instead of using activity activity.getLocalClassName() I prefer use activity.getClass().getSimpleName()

Step 1 in your start activity

Intent intent = new Intent(activity, HistoryDetailsResults.class);
 intent.putExtra(Activity.ACTIVITY_SERVICE, activity.getClass().getSimpleName());
 activity.startActivity(intent);

Step 2 in your destination activity

Intent intent = getIntent();
String startActivity = intent.getStringExtra(Activity.ACTIVITY_SERVICE)
String activityToCompare = YourActivityToCompare.class.getSimpleName()

And what you want to do :

if( startActivity.equalsIgnoreCase(activityToCompare) {
    //Do what you want
}
Nicoolasens
  • 2,871
  • 17
  • 22
0

When you move from one activity to another you can Pass the activity Name as given below

Intent i = new Intent(this, deliveries.class);
i.putExtra("ActivityName", "ActivityOne");
startActivity(i);

and check the activity name in the other activity

Bundle extra = getIntent().getExtras();
String activityName = Long.parseLong(extra.getSerializable("ActivityName")
toString());

I think it can solve your problem

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Arun Kumar
  • 877
  • 3
  • 13
  • 37