how to check which Intent started the activity?

后端 未结 3 1445
深忆病人
深忆病人 2021-02-13 02:11

I have many activities. Each one of them has an intent which refers to the same activity. Is there a way to find out which intent started the activity?

3条回答
  •  逝去的感伤
    2021-02-13 02:39

    I found a solution that doesn't involve passing data from one Activity to another.

    Use startActivityForResult in your calling activity to start the activity:

    ActivityCompat.startActivityForResult(this, new Intent(this, MyActivity.class), 0, null);
    

    In the callee Activity, you can use the following code to detect the calling activity.

        if (getCallingActivity() != null) {
            Log.d(TAG, getCallingActivity().getClassName());
        }
    

    Hope this helps. Cheers.

提交回复
热议问题