I am developing an app that must be able to rotate during the running of an AsyncTask
.
I have tried to implement this pattern which I found in this post. I also implemented the onPause()
and onResume()
and I am able to (re)store my data in the Shared Preferences with json and gson. That part seems to work well.
When my AsyncTask
calls the onPostExecute()
without rotating the screen, everything is OK. However, when the screen is rotated during the AsyncTask
, in the onPostExecute()
my app shows strange behaviour. At a closer look, I noticed while debugging (in Android Studio) my MainActivity
in onPostExecute()
has a different ID than the MainActivity
that is just created with onCreate()
. So during the onPostExecute()
the data is processed with the old MainActivity
while another MainActivity
is already created! And the view is not completely updated until the AsyncTask is finished. This might also be a bad sign...
It is difficult to extract a small project from my code, but as I said, I used the code from this blog. The only real difference is, to prevent memory leakage, instead of having an inner private class DummyTask
I managed to create an (inner) private static class:
private static class MoveMrXAsyncTask extends AsyncTask<Void, Void, Move> {
private WeakReference<TaskCallbacks> activityReference;
...
}
which I got from here.
And this is what my onCreate() looks like:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);
// If the Fragment is non-null, then it is currently being
// retained across a configuration change.
if (mTaskFragment == null) {
mTaskFragment = new TaskFragment();
fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
}
viewModel = new MainActivityViewModel(getApplicationContext(), mTaskFragment);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setViewmodel(viewModel);
}
What am I doing wrong? Have I forgotten something with the Fragment
? I am using the android.app.Fragment. Is this the correct Fragment-class?
Or might the data binding be the problem? Do I have to unbind all data bindings from the old activity and rebind to the new?