How to know if an activity is called using startActivityForResult or simply called by using startActivity?
or should i send some extra data in the Intent to know开发者_如何学JAVA 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..
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.
See getCallingActivity for more details.
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.
example :-
if (getCallingActivity() == null) {
//This Activity was called by startActivity
} else {
//This Activity was called by startActivityForResult
}
I think that you should expose several intents for the same activity in your manifest, then test the calling intent to adapt your behaviour.
Example for your activity intent filter in the manifest:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
and corresponding code in your activity onCreate:
if (getIntent().getAction().equals(Intent.ACTION_VIEW)) {
// do whatever you need to do here
} else if (getIntent().getAction().equals(Intent.ACTION_PICK)){
...
}
you can put a flag like "0" and "1" , putting it in intent, so if "0" then its startActivity or "1" for startActivityForResult... this is simple, isnt it?
精彩评论