how to check which Intent started the activity?

后端 未结 3 609
暖寄归人
暖寄归人 2021-02-13 02:08

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:56

    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.

提交回复
热议问题