43

I have one activity which can be open from more 4 or 5 different activity so i can find from which activity my current activity is called...

If any idea please help me..

Dharmendra
  • 33,296
  • 22
  • 86
  • 129

5 Answers5

47

You might want to add extras to the intent you use to start the activity to indicate where the intent is coming from or what the request is.

For example:

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("caller", "MainActivity");
startActivity(intent);

and then your OtherActivity could detect the "caller" in its onCreate:

String caller     = getIntent().getStringExtra("caller");
Class callerClass = Class.forName(caller);
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • 8
    I have used like this Intent intent = new Intent(this, OtherActivity.class); intent.putExtra("caller", getIntent().getComponent().getClassName()); startActivity(intent); – Dharmendra Mar 30 '11 at 09:07
  • 6
    and then in other activity i have use String className = getIntent().getStringExtra("Caller") and then Class c = Class.forName(className); so i can use that as my destination activity – Dharmendra Mar 30 '11 at 09:09
15

Android has a function for it, if you use startActivityForResult:

ComponentName prev = this.getCallingActivity();

AndroidLover
  • 1,171
  • 1
  • 13
  • 16
leventcinel
  • 358
  • 2
  • 5
  • 39
    `getCallingActivity()` method will only work when you have started the child activity as a `startActivityForResult`. If you will start the child activity as a normal was ie. using `startActivity()` then `getCallingActivity()` method will return null. This is mentioned in the [Android documents](http://developer.android.com/reference/android/app/Activity.html#getCallingActivity()). – Dharmendra Dec 25 '12 at 19:21
2

Put some extras to intent for the different- different activities

eg:

startActivity(new Intent(this, otherActivity.class).putExtra("from" , "previousActivity"));

and get extras in the current activity as

string act = getIntent().getStringExtra("from");

so "act" will indicate you the calling Activity.

2

Create an Interfare And put Values for your Activities Then pass these Values in Intent And get in Child Activity take a look at this piece of code

public interface ActivityRefs {
            public static final int ACTIVITY_1 = 1;
            public static final int ACTIVITY_2 = 2;
            public static final int ACTIVITY_3 = 3;
}

Create Intent and pass value like this in Calling activity

intent.putExtra("callingActivity", ActivityRefs.ACTIVITY_1);

Then in Called activity

int callingActivityName = getIntent().getIntExtra("callingActivity", 0);

        switch (callingActivityName) {
        case ActivityConstants.ACTIVITY_1:
            // Activity3 is started from Activity1
            break;
        case ActivityConstants.ACTIVITY_2:
            // Activity3 is started from Activity2
            break;
        }
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
1

I have solved it, in my case we are on ActivityA to ActivityB then want to go back to ActivityA programmatically, here my code

ActivityA :

String currentActivity;
void onCreate(...){
...
currentActivity = this.getClass().getName();
...
}

Then on your intent (still in ActivityA) :

Intent intent = new Intent(this, ActivityA.class);
intent.putExtra("from", currentActivity);
startActivity(intent);
this.finish();

Now on ActivityB:

Class fromClass;
void onCreate(...){
...
String String from = getActivity().getIntent().getStringExtra("from");
try {
    fromClass = Class.forName(from);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
...
}

Then on your intent(still in ActivityB) for back to ActivityA:

Intent intent = new Intent(this, fromClass);
startActivity(intent);
this.finish();
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76