vityForResult调用活动还是仅通过startActiv

vityForResult调用活动还是仅通过startActiv

本文介绍了如何知道是使用startActivityForResult调用活动还是仅通过startActivity调用活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是我应该在Intent中发送一些额外的数据以了解调用?如果没有像getIntent这样的预定义方法,并对其进行处理?

or should i send some extra data in the Intent to know the call ?if there is no predefined method,like getIntent and do something with it ?

推荐答案

我知道这个问题已经回答了,但是我有一个更好的解决方案.

I know this question is answered already but I have a better solution..

仅通过startActivity()开始活动时,目标活动中的getCallingActivity()方法将返回null.当startActivityForResult()调用它时,它将返回调用活动的名称.

When your activity was started just by startActivity() a getCallingActivity() method in target activity will return null. When it was called by startActivityForResult() it will return name of calling activity.

有关更多详细信息,请参见 getCallingActivity .

See getCallingActivity for more details.

因此,您可以在完成调用活动之前签入活动".如果结果为null,则startActivity()调用Activity,如果结果不为null,则startActivityForResult()调用Activity.而已.

So you can check in Activity before finishing for calling activity. If result is null Activity was called by startActivity() and if result is not null then Activity was called by startActivityForResult(). Thats it.

示例:-

if (getCallingActivity() == null) {
    //This Activity was called by startActivity
} else {
   //This Activity was called by startActivityForResult
}

这篇关于如何知道是使用startActivityForResult调用活动还是仅通过startActivity调用活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:00