I've an activity suppose "Activity A" which I start by two ways
a)StartActivity()
b) StartActivityForResult()
Now I have few methods which are having different behaviours for the way activity started. Now I want to detect that "Activity A" is started for result.
So my question how we can detect that the activity is started for result?
I don't want to send data through intent. Any other way more generalized?
Thank You.
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.
Depending on what you are trying to do (update the current activity from a service?). You could just register a static listener in the service in your activity onStart method then the correct listener will be available when your service wants to update the UI.
Stay organized with collections Save and categorize content based on your preferences. A launcher for a previously-prepared call to start the process of executing an ActivityResultContract .
onActivityResult - resultCode is always 0.
From this answer
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.
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