How to use onActivityResult method from other than Activity class

onactivityresult resultcode
when is onactivityresult called
onactivityresult in adapter
setresult android
onactivityresult overrides nothing
onactivityresult in fragment
android request code list
onactivityresult kotlin

I am creating an app where i need to find current location of user .

So here I would like to do a task like when user returns from that System intent, my task should be done after that.(Displaying users current location)

So i am planning to use OnActivityResult().

protected void onActivityResult(int requestCode, int resultCode, Intent data) {


But the problem is that I don't know how can I use that method in a class which is not extending Activity.

Please some one give me idea how can i achieve this?

You need an Activity on order to receive the result.

If its just for organisation of code then call other class from Activty class.

public class Result {
    public static void activityResult(int requestCode, int resultCode, Intent data){

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

Getting a result from an activity, startActivityForResult method, requires a result from the second activity (activity to be invoked). When you start an activity with startActivityForResult method from an activity, only the caller will recive the result. So you could handle the result and pass it to the task or update the ui of that activity: int MY_REQUEST_ID = 1; public void onClick(){ //Select a contact.

Create an inner class in the non Activity class and define your activity results handler therein:

class singletonActivity extends Activity{
  protected void onActivityResult(...){
    // do whatever ..

intantiate it to call startActivityForResult

Activity actv = new singletonActivity(..)
actv.startActivityForResult(intent ..)

your handler will be called. :)

ps: you may have to include some overrides. just leave them empty.

pps: this is old school java mouseListenerAdapter style ~Oo>

Android StartActivityForResult Example, Which method is used to call another activity when we expect to get something back from the called activity? This article demonstrates the working and use of startActivityForResult() and onActivityResult() methods of android. startActivityForResult() method is useful when you want to start another

You can't call this method out of his scope.

protected void onActivityResult (int requestCode, int resultCode, Intent data)

If the method is protected like this case, you can see the table of Access Levels to know how to proceed.

|                     ACCESS LEVELS                         |
|      Modifier    |  Class  | Package | Subclass |  World  |
|      public      |    Y    |    Y    |    Y     |    Y    |
|      protected   |    Y    |    Y    |    Y     |    N    |
|      no modifier |    Y    |    Y    |    N     |    N    |
|      private     |    Y    |    N    |    N     |    N    |

As you can see, this method only can be called from* package, Activity and their subclasses.


You need to do something like this:

We have a class ImagePicker for selecting a image from Gallery or Camera or Delete it. This class need to call onActivityResult if user wants to delete image (We don't need to start an Activity for a result that we already know).

public class ImagePicker {
    private ImagePickerDelegate delegate;

    public ImagePicker (ImagePickerDelegate delegate) {
        this.delegate = delegate;

    //Will explain this two methods later
    public void show() {
        //Some code to show AlertDialog

    public void handleResponse(Intent data) {
        //Some code to handle onActivityResult

    //Our interface to delegate some behavior 
    public interface ImagePickerDelegate {
        void onImageHandled(Bitmap image);
        void onImageError();
        void onImageDeleted();

For using this class in our Activity, we need to implement the delegate methods and pass our activity as the delegate of ImagePicker:

public class MyActivity extends Activity implements ImagePicker.ImagePickerDelegate {
    ImagePicker imagePicker;    

    public void selectImage () {
        imagePicker = new ImagePicker(this);;

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ImagePicker.REQUEST_IMAGE_PICKER && resultCode == RESULT_OK) {
        super.onActivityResult(requestCode, resultCode, data);

    public void onImageHandled(Bitmap image) {
        //handle image resized

    public void onImageError() {
        //handle image error
        Toast.makeText(this, "Whoops - unexpected error!", Toast.LENGTH_SHORT).show();

    public void onImageDeleted() {
        //handle image deleted

Finally, we need to make thous delegate methods to be called, and that happen in show() and handleResponse(Intent data):

//The show method create and dialog with 3 options,
//the important thing here, is when an option is selected
public void show() {
    //Inflating some views and creating dialog...

    NavigationView navView = (NavigationView)viewInflated.findViewById(;
    navView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() {
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    activity.startActivityForResult(pickPhoto , REQUEST_IMAGE_PICKER);
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    activity.startActivityForResult(takePicture, REQUEST_IMAGE_PICKER);
                    delegate.onImageDeleted(); //send response to activity
            return true;

    //Show dialog...

//this method is called from onActivityResult
public void handleResponse(Intent data) {
    try {
        //Retrieve and resize image...
        delegate.onImageHandled(image); //send the image to activity
    } catch (Exception e) {
        delegate.onImageError(); //send error to activity

At the end, what we have, is a class that can call a method in your Activity instead of onActivityResult, but when you get a result in onActivityResult, you need to handle it in that class

Android - Activities, onStop() This callback is called when the activity is no longer visible. In this Android tutorial we will learn about the onActivityResult method. This method will allow us to catch information from the activity that we are lookin

You need to register an Activity to this class and then use OnActivityResult() for that activity.

Android startActivityForResult Example, onDestroy() This callback is called before the activity is destroyed by the system. By the help of android startActivityForResult() method, we can get result from another activity. By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

When you start an activity with startActivityForResult method from an activity, only the caller will recive the result.

So you could handle the result and pass it to the task or update the ui of that activity:

int MY_REQUEST_ID = 1;

public void onClick(){
    //Select a contact.
             new Intent(Intent.ACTION_PICK,
             new Uri("content://contacts")),

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if(requestCose == MY_REQUEST_ID && resultCode == SUCCESS) {
         MyAsyncTask task = new AsyncTask(requestCode, resultCode, data);
         // or update the UI
         textView.setText("Hi, activity result: "+ resultCode);

startActivityForResult() VS startActivity() - Nikhil Dhyani, onRestart() This callback is called when the activity restarts after stopping it. Once Activity was navigated back, the result will be sent to Activity's onActivityResult with the modified requestCode which will be decoded to original requestCode + Fragment's identity. After that, Activity will send the Activity Result to that Fragment through onActivityResult. And it's all done.

How to make onActivityResult get called on Nested Fragment :: The , , the startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed. However, sometimes we might want to get a result back from an activity when it ends. Some questions about onActivityResult and startActivityForResult but then it comes back to Unity without calling onActivityResult method. In this activity I

Getting a Result from an Activity, () is used to start another activity from parent activity and then parent activity expects some data in return from child activity. For example suppose you want to select a picture from a gallery and set is as your dp. We can call one activity from another by using Intents. Intent is one of the main building block which provides an abstract description of an operation to be performed. startActivity(intent) method belongs to your Activity class and can be used for starting a new activity.

Starting an activity with result the clean way (Example), Android StartActivityForResult Example with examples of Activity and Intent, Fragments, The android startActivityForResult method, requires a result from the second Right click on the package inside the src -> New -> Other ->Android Activity. Intent(MainActivity.this,SecondActivity.class);; startActivityForResult(​intent,  And that's usually done during the call to the activity's onCreate method when you recreate the activity. Now, note that these methods have been deprecated in favor of other methods in the Fragment class. And we haven't discussed fragments yet, but we'll come back to it in a later lesson when we do.

  • follow this link… it may helps you
  • You cannot call that method in other classes. You should create the instance of that class in the activity and call the processing method there.
  • You can create blank transparent activity for this, and return result to your task from it's onActivityResult().
  • Where are you calling this from??
  • i couldn't understand your logic. Will you give a demo for that
  • I am doing something similar just for organising code for Facebook login, but the onActivityResult() in Fragment doesn't get called after successful login attempt. Can anyone please help?
  • Good idea, i was working on some third party project where i was not having access to main project code. your answer may solve my issue.
  • This gives an error Unable to find explicit activity class {...} have you declared this activity in your AndroidManifest.xml?. How should this be added to AndroidManifest.xml?
  • This is the correct answer to this and all those similar questions on SO. It should have more upvotes.
  • And where is the onActivityResult method?
  • This seems to be an uncompleted code. From your question it is understood that you need "OnActivityResult" method, where is it in your answer?