Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if an activity is called using startActivityForResult or simply called by using startActivity?

Tags:

android

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 ?

like image 867
Harinder Avatar asked Sep 27 '11 05:09

Harinder


People also ask

Is compulsory to implement when startActivityForResult () has been called?

No it is not mandatory.

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.

Which 2 methods do we override when we need to get a result from an activity started from an intent?

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.

What can I use instead of startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.


1 Answers

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
}
like image 107
Pankaj Kumar Avatar answered Oct 21 '22 06:10

Pankaj Kumar