Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onActivityResult in another class

I want to use onActivityResult in a class which not extends activity but this class has a global variable as an instance of activity. This class is similar to this :

public class myClass(){
    public Activity myActivity;
    .
    .
    .
    // I want to add onMyActivityResult here
}

I am looking for something like this:

myActivity.setOnActivityResult...

Thanks!

like image 281
Payam Roozbahani Fard Avatar asked Jan 09 '16 09:01

Payam Roozbahani Fard


People also ask

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

How do I use onActivityResult?

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 Startactivity for results?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.


2 Answers

you could do the following

public class myClass(){

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

          //right your code here .
   }
}

and in your CLASS EXTENDS ACTIVITY write this inside the onActivityResult Method :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       myClass.activityResult(requestCode,resultCode,data);
    }

thnx to this answer :https://stackoverflow.com/a/21780532/3818437

like image 104
mhdjazmati Avatar answered Sep 30 '22 13:09

mhdjazmati


This is not possible to override of instance onActivityResult() but it is possible to do the way below. The major idea is to create your class which calls your custom function inside the code; In this case

That is, your activity class have to be extended by ABase not by Activity:

public class yourClass extends ABase{
//your usual code is here.
}

Then, ABase looks like this:

public class ABase extends Activity {
private Callable<Void> clb;//this function will be called
protected void onActivityResult(int requestCode, int resultCode,
                Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (clb != null) {
                        try {
                        clb.call();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
}
public void setMyCallable(Callable<Void> callable){
this.clb = callable;
}
}

in your public myClass use public ABase act instead of public Activity act; there is a setSomeFunction() for example to use:

public class myClass {
public ABase act;
public static setSomeFunction() {
    if (act != null) {
        Callable<Void> clb = new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    Log.d(TAG, "is Called!");
                    return null;
                }
            }
        act.setMyCallable(clb);

        }
    }
}
like image 22
Vyacheslav Avatar answered Sep 30 '22 15:09

Vyacheslav