39

I have an activity which is called by a few other activities. For example, I have Activity1, Activity2, and Activity3. Activity1 calls Activity2 and passes a parameter. Activity3 also calls Activity2 and passes a parameter.

Now, based on the calling activity, Activity2 performs some task. How do I know which activity is calling Activity2?

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
Reza.Hoque
  • 2,690
  • 10
  • 49
  • 78
  • possible duplicate of [How to know if an activity is called using startActivityForResult or simply called by using startActivity?](http://stackoverflow.com/questions/7564461/how-to-know-if-an-activity-is-called-using-startactivityforresult-or-simply-call) – Pankaj Kumar Apr 11 '14 at 14:20
  • do you mean it?? how can a question be a duplicate of another which has been asked few months later ?? – Reza.Hoque Apr 14 '14 at 11:08
  • First of all don't be panic about duplication. I am not pointing any thing bad from your side. I don't thing that duplication depends on the date of question has been asked. See a thread http://stackoverflow.com/questions/6812003/difference-between-oncreate-and-onstart/6812066 where I answered, question was asked many month before target question. – Pankaj Kumar Apr 14 '14 at 11:22

8 Answers8

63

If you start the activity with startActivityForResult(Intent, int), then you can get calling activity by getCallingActivity().getClassName().

Dimitar
  • 4,402
  • 4
  • 31
  • 47
Zain Ali
  • 15,535
  • 14
  • 95
  • 108
42

A. If you can use startActivityForResult

As per Zain Ali's answer below: If you can start Activity with startActivityForResult() then you can get name of calling Activity class by this.getCallingActivity().getClassName();

B. If you can not use startActivityForResult

If you can not use startActivityForResult(), then you can use following method: You can pass additional parameter in intent, check the value in activity and act accordingly.

1) Define an interface or constants class to define integer constants to indicate calling activity

public interface ActivityConstants {
            public static final int ACTIVITY_1 = 1001;
            public static final int ACTIVITY_2 = 1002;
            public static final int ACTIVITY_3 = 1003;
}

2) Add extra parameter in intent while calling Activity2.

        Intent act2 = new Intent(context, Activity2.class);
                act2.putExtra("calling-activity", ActivityConstants.ACTIVITY_1);
    // or ActivityConstants.ACTIVITY_3 if called form Activity3
startActivity(act2);

3) Check the value of this extra parameter in Activity2 and act accordingly..

int callingActivity = getIntent().getIntExtra("calling-activity", 0);

        switch (callingActivity) {
        case ActivityConstants.ACTIVITY_1:
            // Activity2 is started from Activity1
            break;
        case ActivityConstants.ACTIVITY_3:
            // Activity2 is started from Activity3
            break;
        }
Community
  • 1
  • 1
Udayan
  • 1,374
  • 1
  • 11
  • 20
9

In your calling activity (FirstActivity):

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("classFrom", FirstActivity.class.toString());
startActivity(i);

And add the following code in the onCreate of the called activity (SecondActivity):

Bundle bundle = getIntent().getExtras();

if (bundle.getString("classFrom").equals(FirstActivity.class.toString())) {
     //Do some task
}

Notice that you should be carefully because the bundle object can't be null when you perform "b.getString("classFrom")".

Carlos
  • 1,319
  • 2
  • 17
  • 20
2

You could pass an additional parameter that specifies the calling Activity.

dave.c
  • 10,910
  • 5
  • 39
  • 62
0

I successfully use: (Activity).getLocalClassName()

TrippinBilly
  • 957
  • 2
  • 10
  • 19
0

Pass anything(String/ int etc.) to putExtra and base on that do the your work like

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
     intent.putExtra("PARENT_ACTIVITY_REF", "ParentActivityIsA");
     startActivity(intent);

And then receive in child like

String parentActivityRef = intent.getStringExtra("PARENT_ACTIVITY_REF");

then

if (parentActivityRef.equals("ParentActivityIsA"){
              // do your work here
}else if ...{
             // ...
}else{
     //...
}
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
0

I'm Using This line

    if (((((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).baseActivity)).compareTo(new ComponentName(getPackageName()
                    , AnyActivityWantToCheck.class.getName())) == 0){
// do somthing .....
    }

i hope it work with you

ahmad
  • 85
  • 1
  • 4
  • Note: this method is only intended for debugging and presenting task management user interfaces. and is deprecated. – user1506104 Apr 08 '18 at 08:00
0

Another solution, using companion object as in the following example:

class MainActivity: BaseActivity {
    ...
    LoginActivity.callerActivity = this
    it.context.startActivity(Intent(getActivity(), LoginActivity::class.java))
    ...
}

class LoginActivity : BaseActivity() {
    companion object {
        var callerActivity: AppCompatActivity? = null
    }

    ...

    callerActivity?.let {
        it.callSomething()
    }
}
Amr
  • 2,160
  • 1
  • 15
  • 8