Android get previous activity
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.
Solution 1:
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.
Solution 2:
You can use:
public ComponentName getCallingActivity()
to know which Activity called your current Activity
.
Solution 3:
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
}