AsyncTaskLoader: onLoadFinished not called after orientation change

I have an AsyncTaskLoader with a long task, when during bootloader operation my activity is destroyed due to a change in orientation, the onLoadFinished not called.

Can I somehow "reconnect" the bootloader to my new activity / callback?

Here is my (simplified) Activity :

 public class DashboardActivity extends BaseActivity { StartupCallback startupCallback; boolean loading = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.empty_viewpager); startupCallback = new StartupCallback(); if (!loading){ getSupportLoaderManager().initLoader(GlobalApp.giveId(), null, startupCallback); loading = true; } } private class StartupCallback implements LoaderManager.LoaderCallbacks<Boolean> { @Override public void onLoadFinished(Loader<Boolean> loader, Boolean succ) { Log.d("LOG", "onLoadFinished"); } @Override public Loader<Boolean> onCreateLoader(int id, Bundle args) { return new StartupLoader(getApplicationContext()); } @Override public void onLoaderReset(Loader<Boolean> loader) { } } } 

I can’t just start another bootloader with a new callback because the bootloader does the database stuff, and two loaders running on the same database will crash the application.

+4
source share
3 answers

When you change the orientation, your activity is destroyed and recreated, but the bootloaders are not.

It is worth noting that calling initLoader () does not necessarily launch another bootloader. If the loader addressed by the specific identifier exists, it is reused by the LoaderManager and your callbacks join it. That way you can remove the condition (! Loading) and call initLoader in every onCreate () callback. Just make sure the bootloader id is the same.

See also Bootloader Manual

+9
source

From the documentation, if the identifier matches the identifier that you first passed, it should return the current bootloader, not a new one: android documents

+1
source

Adding android:configChanges="orientation|screenSize" to my activity did the trick for me.

-eight
source

Source: https://habr.com/ru/post/1479731/


All Articles