Apps-android-commons: Media details lost on device rotation

Created on 30 Sep 2017  路  4Comments  路  Source: commons-app/apps-android-commons

I happened to be testing the app on a tablet (real hardware) and rotated the screen. I then verified and it also occurs on a phone (emulator) -

media-details-landscape

config-change-bug

This is a bug that is present on current _master_ but not present in the released version in the Google Play Store.

assigned bug media details

All 4 comments

Looking into things a little more: each of the media pager adapter fragments reach back to the ContributionsActivity for data. After rotation, the adapter in contributionsList is null.

@Override
public Media getMediaAtPosition(int i) {
    if (contributionsList.getAdapter() == null) {
        // not yet ready to return data
        return null;
    } else {
        return Contribution.fromCursor((Cursor) contributionsList.getAdapter().getItem(i));
    }
}

The problem isn't so much that the adapter is null, it's that we are relying on the adapter to be our source of truth. Android _configuration change_ will drop and recreate the GUI and steps need to be taken to hold data in a location that will persist across configuration changes. Since we have data that is shared between the ContributionsActivity and multiple MediaDetailFragment instances, there probably should be some kind of external _view model_ that they share.

Google haven't yet released their "architecture components" but they are close - the release notes on 9/21 say

We are not planning any more API changes. Unplanned changes might happen, but the bar for changing any API before 1.0.0 stable is very high and unlikely to happen

so it might still be early days to adopt them directly, but they do have an excellent write up of the problem of configuration change, and what the role of a _view model_ should be - https://developer.android.com/topic/libraries/architecture/viewmodel.html

Also, it doesn't inspire me with confidence when a comment in the MediaDetailPagerFragment says,

if (savedInstanceState != null) {
    final int pageNumber = savedInstanceState.getInt("current-page");
    // Adapter doesn't seem to be loading immediately.
    // Dear God, please forgive us for our sins
    view.postDelayed(() -> {
        ...
    }, 100);
} else {
    ...
}

I am working to fix this issue.

@JohnKal great, thanks! :)

This bug seems solved.

Was this page helpful?
0 / 5 - 0 ratings