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.

- 3,851
- 2
- 28
- 46

- 1,455
- 8
- 23
- 39
-
+1 - agree with @variant-45, put an `int` or anything else as flag from the caller `Activity` – Adil Soomro Nov 14 '11 at 09:35
-
Add activity flag to identify than put intent to mainActivity and please dear put code in ques.. so any one can idea fro reply – Jigar Patel Nov 14 '11 at 09:37
-
Possibly a duplicate: http://stackoverflow.com/questions/4967799/how-to-know-the-calling-activity-in-android – Aman Deep Gautam Mar 25 '15 at 12:18
5 Answers
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.

- 37,824
- 19
- 133
- 148

- 67,150
- 23
- 161
- 242
-
1
-
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
You can use:
public ComponentName getCallingActivity()
to know which Activity called your current Activity
.

- 8,202
- 5
- 33
- 48
-
42Only works if you start the activty with `startActivityForResult()`, not `startActivity()`. – Anubian Noob Jul 24 '15 at 22:09
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
}

- 4,076
- 7
- 48
- 67
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
}

- 2,871
- 17
- 22
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

- 37,824
- 19
- 133
- 148

- 877
- 3
- 13
- 37