如何在另一个类中使用onActivityResult

时间:2016-01-09 09:01:38

标签: android onactivityresult

我想在不扩展活动的类中使用onActivityResult,但是这个类有一个全局变量作为活动的实例。这个类与此类似:

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

我正在寻找类似的东西:

myActivity.setOnActivityResult...

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以执行以下操作

public class myClass(){

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

          //right your code here .
   }
}

并在 CLASS EXTENDS ACTIVITY 中将此内容写入onActivityResult方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       myClass.activityResult(requestCode,resultCode,data);
    }
答案是:https://stackoverflow.com/a/21780532/3818437

答案 1 :(得分:1)

这不可能覆盖实例onActivityResult(),但可以按照以下方式执行。 主要的想法是创建一个在代码中调用自定义函数的类; 在这种情况下

也就是说,您的活动类必须通过ABase而非活动扩展:

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

然后,ABase看起来像这样:

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;
}
}

在您的公开myClass中使用public ABase代替public Activity act; 例如,使用setSomeFunction()

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);

        }
    }
}

答案 2 :(得分:0)

public class MyActivity extends Activity {

    public interface IResult {
        void call(Intent data);
    }

    public IResult r;


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

        if (resultCode != RESULT_OK) return;
        if (r == null) return;

        r.call(data);
    }

    void startZ() { Z.startZ(this); }
    void startY() { Y.startY(this); }
}

Z

class Z {
    public static void startZ(MyActivity a) {
        a.r = data -> {
            //...YOUR CODE HERE...
        };

        a.startActivityForResult(
                new Intent(a, ZActivity.class),
                request_code___not_needed_because_only_one_activity_lives);
    }
}

Y

class Y {
    public static void startY(MyActivity a) {
        a.r = data -> {
            //...YOUR CODE HERE...
        };

        a.startActivityForResult(
                new Intent(a, YActivity.class),
                request_code___not_needed_because_only_one_activity_lives);
    }
}