Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting activity is started for result

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.

like image 862
Sandip Jadhav Avatar asked Dec 24 '12 08:12

Sandip Jadhav


People also ask

How do I know if my activity has started for results?

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.

How can I check if an activity is already running before starting it?

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.

What is activity result launcher in Android?

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 .

What is result code in onActivityResult?

onActivityResult - resultCode is always 0.


1 Answers

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
}
like image 89
Joaquin Iurchuk Avatar answered Oct 02 '22 10:10

Joaquin Iurchuk