how to know the calling activity in android

2019-01-07 00:09发布

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

Now based on the calling activity, Activity2 performs some task. But how do I know which activity is calling Activity2?? can anybody plz help me??

7条回答
Fickle 薄情
2楼-- · 2019-01-07 00:39

I successfully use: (Activity).getLocalClassName()

查看更多
干净又极端
3楼-- · 2019-01-07 00:44

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

查看更多
看我几分像从前
4楼-- · 2019-01-07 00:51

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

查看更多
迷人小祖宗
5楼-- · 2019-01-07 00:53

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{
     //...
}
查看更多
聊天终结者
6楼-- · 2019-01-07 00:56

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

查看更多
来,给爷笑一个
7楼-- · 2019-01-07 00:58

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;
        }
查看更多
登录 后发表回答