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!
To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment.
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.
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.
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.
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
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With