如何从非活动类调用 startactivityforresult 以获取结果 [英] How to call startactivityforresult from a non-activity class to get the resuts

查看:31
本文介绍了如何从非活动类调用 startactivityforresult 以获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从非活动类调用 startActivityForResult() 来获取结果?

Is it possible to call startActivityForResult() from a non-activity class to get the results?

场景是这样的:

我有一个 NonActivity 类(它不是从 Activity 派生的,因为它不是一个 UI).这个类将有一堆函数(基本上是步骤)来运行.其中一个步骤需要显示 UI(Activity),然后获取结果(用户输入内容).然后就可以在接下来的步骤中使用这些数据了.

I have a class NonActivity (it doesn't derive from Activity as its not a UI). This class will have bunch of functions(steps basically) to run. One of the steps requires to show UI(Activity) and then get the result (user enter something). Then been able to use that data in next following steps.

如何在不从活动类派生的情况下实现这一点,因为我没有 UI 组件?此外,由于我不想从活动类派生,这意味着我无法覆盖 OnActivityResult().结果实际上来自哪里?

How can this be achieved without deriving from activity class as I don't have UI component? Also since I don't want to derive from activity class that means I cannot override OnActivityResult(). Where results actually come from?

推荐答案

startActivityForResult() 只能从真实的屏幕活动中使用,因为它是 Activity 中的一个方法.请重新设计您的应用程序,以便用户界面由活动驱动.

startActivityForResult() is only available from real on-screen activities, since it is a method in, well, Activity. Please redesign your application so that the user interface is driven from activities.

另一方面,如果您的非 Activity 类是从屏幕上的 Activity 初始化和使用的,您可以将该 Activity 的实例作为构造函数中的参数传递给您的类,并使用它来启动其他 Activity.

On the other hand, if your non Activity class is initialized and used from an onscreen Activity, you could pass that instance of the Activity to your class as a parameter in the constructor and use it to launch other Activities.

不过要小心.使用此方法会增加内存泄漏的风险,因为外部类(在我的示例中为 Utils)可能会在 Activity 消失后保留对它的引用.

Be careful though. Using this method increases the risk of a memory leak, as the external class (Utils in my example) might keep a reference to the Activity even after its gone.

如果您只想访问数据,那么您可以尝试将其写入 SharedPreferences 或数据库或某些文件,然后使用应用程序上下文(再次通过构造函数传入)来读取它.这降低了内存泄漏的风险.类似的东西:

If all you want to do is access data, then you could try writing it to SharedPreferences or a Database or some files and then using the application context (passed in via a constructor again) to read it. This reduces the risk of a memory leak. Something like:

MyApiClass myApiClass = new MyApiClass(getApplicationContext());

示例代码

主要活动:

public class Main extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Utils util = new Utils(this);
        util.startTest();

    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        Toast.makeText(this, "onActivityResult called", Toast.LENGTH_LONG).show();

        super.onActivityResult(arg0, arg1, arg2);
    }

}

Utils 类(为结果而启动):

Utils class (which launches for result):

public class Utils {

    Activity activity;

    public Utils(Activity ac) {
        activity = ac;
    }

    public void startTest() {
        Intent i = new Intent(activity, Test.class);
        activity.startActivityForResult(i, 1);

    }

}

测试活动:

public class Test extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();

        this.setResult(Activity.RESULT_OK);
        this.finish();

    }

}

这篇关于如何从非活动类调用 startactivityforresult 以获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆