屏幕旋转后Android从嵌套的AsyncTask onPostExecute保存数据

时间:2012-04-30 21:25:21

标签: android android-asynctask screen-rotation

我花了很多时间寻找解决方案并需要帮助。

我的Android应用程序Activity中有一个嵌套的AsyncTask,我希望允许用户在处理过程中旋转手机,而无需启动新的AsyncTask。我尝试使用onRetainNonConfigurationInstance()和getLastNonConfigurationInstance()。

我能够保留任务;但是在旋转之后它不会将onPostExecute()的结果保存到外部类变量中。当然,我尝试过吸气剂和二传手。当我在onPostExecute中转储变量时,它就可以了。但是当我尝试从onClick侦听器访问变量时,它就是null。

也许代码会让问题清楚。

public class MainActivity extends BaseActivity {

    private String possibleResults = null;
    private Object task = null;

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

            this.task = getLastNonConfigurationInstance();

            setContentView(R.layout.menu);

            if ((savedInstanceState != null)
                            && (savedInstanceState.containsKey("possibleResults"))) {
                    this.possibleResults = savedInstanceState
                                    .getString("possibleResults");
            }

            if (this.possibleResults == null) {
                    if (this.task != null) {
                            if (this.task instanceof PossibleResultWebService) {
                                    ((PossibleResultWebService) this.task).attach();
                            }

                    } else {
                            this.task = new PossibleResultWebService();
                            ((PossibleResultWebService) this.task).execute(this.matchToken);
                    }
            }

            Button button;
            button = (Button) findViewById(R.id.menu_resultButton);
            button.setOnClickListener(resultListener);
    }

    @Override
    protected void onResume() {
            super.onResume();
    }

    OnClickListener resultListener = new OnClickListener() {
            @Override
            public void onClick(View v) {

                    Spinner s = (Spinner) findViewById(R.id.menu_heatSpinner);
                    int heatNo = s.getSelectedItemPosition() + 1;
                    Intent myIntent = new Intent(MainActivity.this,
                                    ResultActivity.class);
                    myIntent.putExtra("matchToken", MainActivity.this.matchToken);
                    myIntent.putExtra("heatNo", String.valueOf(heatNo));
                    myIntent.putExtra("possibleResults",
                                    MainActivity.this.possibleResults);

                    MainActivity.this.startActivityForResult(myIntent, ADD_RESULT);
            }
    };

    private class PossibleResultWebService extends AsyncTask<String, Integer, Integer> {

            private ProgressDialog pd;
            private InputStream is;
            private boolean finished = false;
            private String possibleResults = null;

            public boolean isFinished() {
                    return finished;
            }

            public String getPossibleResults() {
                    return possibleResults;
            }

            @Override
            protected Integer doInBackground(String... params) {
                // quite long code
            }

            public void attach() {
                    if (this.finished == false) {
                            pd = ProgressDialog.show(MainActivity.this, "Please wait...",
                                            "Loading data...", true, false);
                    }
            }

            public void detach() {
                    pd.dismiss();
            }

            @Override
            protected void onPreExecute() {
                    pd = ProgressDialog.show(MainActivity.this, "Please wait...",
                                    "Loading data...", true, false);
            }

            @Override
            protected void onPostExecute(Integer result) {
                    possibleResults = convertStreamToString(is);
                    MainActivity.this.possibleResults = possibleResults;

                    pd.dismiss();
                    this.finished = true;
            }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

            super.onSaveInstanceState(outState);

            if (this.possibleResults != null) {
                    outState.putString("possibleResults", this.possibleResults);
            }

    }

    @Override
    public Object onRetainNonConfigurationInstance() {
            if (this.task instanceof PossibleResultWebService) {
                    ((PossibleResultWebService) this.task).detach();
            }
            return (this.task);
    }

}

1 个答案:

答案 0 :(得分:0)

这是因为每次实例化Activity时都会创建OnClickListener(所以每次获得一个新的,新的OuterClass.this引用时),但是你要在Activity实例化之间保存AsyncTask并保持对它的引用首先通过引用OuterClass.this来实例化Activity。

有关如何正确执行此操作的示例,请参阅https://github.com/commonsguy/cw-android/tree/master/Rotation/RotationAsync/

你会看到他在RotationAwareTask中有一个attach()和detach()方法来解决这个问题。

要确认AsyncTask中的OuterClass.this引用将始终指向第一个实例化的Activity,如果你将它保持在屏幕方向更改之间(使用onRetainNonConfigurationInstance),那么你可以使用一个静态计数器,每次默认构造函数都会增加并保留一个实例级变量,该变量在每次创建时设置为计数,然后打印出来。