0

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?

ffonz
  • 1,304
  • 1
  • 11
  • 29
  • 1
    AFAIK Asyn task as a default strange behaviour while screen config changes try [AsynTaskLoader](https://developer.android.com/reference/android/content/AsyncTaskLoader.html) to overcome the issues. Take a look at this [Link](https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-concepts/content/en/Unit%203/71c_asynctask_and_asynctaskloader_md.html) – Mohamed Mohaideen AH Dec 28 '17 at 13:42
  • Thanks for the tip. It seems that your right and I need a AsynTaskLoader. I will let you know the result. – ffonz Dec 28 '17 at 16:44
  • welcome glad to help. Happy coding:) – Mohamed Mohaideen AH Dec 29 '17 at 15:01
  • @Mohamed Mohaideen AH: I have tried to implement an AsyncTaskLoader (from https://medium.com/@sanjeevy133/an-idiots-guide-to-android-asynctaskloader-76f8bfb0a0c0). It is similar to the one in your link. When I run it normally, it works. But when I rotate the screen during the loadInBackground(), it doesn't. Through logging I see that the loadInBackground() is finished, but the onLoadFinished() is never called anymore. What am I doing wrong? – ffonz Dec 30 '17 at 17:14
  • Found it! I didn't realize that I had to trigger the loader again in onResume(). – ffonz Dec 30 '17 at 19:00

0 Answers0