从Application到Launcher Activity获取异步响应数据

时间:2016-06-18 23:28:39

标签: android android-activity initialization

我想在我的应用启动时发出初始化请求。然后我想在MainActivity中使用响应。我不想在Activity中发出该请求,然后在手机旋转时处理Activity生命周期。

所以我在考虑从Application派生并在那里提出请求。但是,将响应数据发送到我的启动器活动的最佳方法是什么?

这里有“最佳实践”解决方案吗?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用Event Bus之类的库,以便在请求任务完成后接收活动中的数据。通过这样做,您不必担心调用的来源或者您的活动是否被轮换或重新创建。

如果数据特定于您的MainActivity,我会建议从那里触发请求,以保持耦合。

答案 1 :(得分:1)

如果您正在寻找最佳做法,则不应为此扩展应用类。

有很多方法可以在屏幕旋转时保持请求状态。

考虑使用retained Fragment。深入讨论了这种方法:
Understanding Fragment's setRetainInstance(boolean)
Further understanding setRetainInstance(true)

您需要做的就是:

1。片段类

public class RequestFragment extends Fragment {

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

        // This will be a guarantee that request is sent only once, 
        // because fragment won't be recreated on screen rotation:
        setRetainInstance(true);

        // Pereform sending request here.
    }

}

2。活动类

public class MainActivity extends AppCompatActivity {

    private final static TAG_FRAGMENT = "persistent_fragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();

        // Create fragment if it doesn't exist yet.
        if (fm.findFragmentByTag(TAG_FRAGMENT) == null) {
            fm.beginTransaction()
                    .add(new RequestFragment(), TAG_FRAGMENT)
                    .commit();
        }
    }

}


但是,如果您强烈决定在Application onCreate()方法中执行请求,则必须实现一个可观察对象,该对象响应订阅它的活动,因为您无法从Application类访问Activity。

你可以试试这个:

1。 ResponseObservable类

public class ResponseObservale {

    private MainActivity activity;
    private Response response;

    public void sendRequest() {
         // perform your async request here.
    }

    /* 
     * Consider this method as a point where the response is delivered.
     * It can be done in onPostExecute of AsyncTask or somewhere else,
     * depending on your implementation.
     */
    public void onResponse(Response response) {
        this.response = response;
        publishResponse();
    }

    public void onActivityCreated(MainActivity activity) {
        this.activity = activity;
        if (response != null) {
            publishResponse();
        }
    }

    private void publishResponse() {
        if (activity != null) {
            activity.obtainResponse(response);
        }
    }

    public void onActivityDestroy() {
         activity = null;
    }

}

2。申请类

public class MyApplication extends Application {

    private ResponseObservable observable;

    @Override
    public void onCreate() {
        super.onCreate();
        observable = new ResponseObservable();
        observable.sendRequest();
    }

    public ResponseObservable getObservable() {
        return observable;
    }

}

3。活动类

public class MainActivity extends AppCompatActivity {

    private ResponseObserbale observable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyApplication app = (MyApplication) getApplicationContext();
        observable = app.getObservable();
        observable.onActivityCreated(this);
    }

    public void obtainResponse(Response response) {
        // Manage your response here.
    }

    @Override
    protected void onDestroy() {
        observable.onActivityDestroy();
    }

}


别忘了在AndroidManifest.xml中声明您的Application类:

<application
    android:name="com.yournamespace.appname.MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name">