Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If Activity Has Been Called for Result

Is it possible to know if some activity has been called for result, using startActivityForResult() or if was only started using startActivity()?

I need to control this, if its called for result the behaviour will be different.

like image 673
TiagoM Avatar asked May 25 '13 20:05

TiagoM


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 do I know if my activity is paused?

When the event occurs, just call this method in the base class. The correct 'active' class will handle it then. The class itself can then check if it is not Paused() . Show activity on this post.

How do you send data into activity results?

Put the data that you want to send back to the previous activity into an Intent . The data is stored in the Intent using a key-value pair. Set the result to RESULT_OK and add the intent holding your data. Call finish() to close the Second Activity.


1 Answers

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. This is who the data in setResult() will be sent to. You can use this information to validate that the recipient is allowed to receive the data.

Note: if the calling activity is not expecting a result (that is it did not use the startActivityForResult(Intent, int) form that includes a request code), then the calling package will be null.

Returns

The ComponentName of the activity that will receive your reply, or null if none.

like image 72
lopisan Avatar answered Sep 18 '22 18:09

lopisan