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 ?
No it is not mandatory.
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 Docs for getCallingActivity() : Return the name of the activity that invoked this activity.
The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With