Skip to content Skip to sidebar Skip to footer

Pageradapter Changed Adapters Content With Notifying

So first things first, here's the error that I'm getting: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling Pager

Solution 1:

It's because you are using this line viewPager.setOffscreenPageLimit(4);, It's mean viewpager won't re-create the screen in all 4 pages. However, there is a function to detect if your screen has been visible completely, it's call setUserVisibleHint(). You just need to use like below: //For the Fragment case

@OverridepublicvoidsetUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
         //TODO notify your recyclerview data over here
        }
    }

EDIT:

For the Activity case: If targeting API level 14 or above, one can use

android.app.Application.ActivityLifecycleCallbacks

publicclassMyApplicationextendsApplicationimplementsActivityLifecycleCallbacks {
    privatestaticboolean isInterestingActivityVisible;

    @OverridepublicvoidonCreate() {
        super.onCreate();

        // Register to be notified of activity state changesregisterActivityLifecycleCallbacks(this);
        ....
    }

    publicbooleanisInterestingActivityVisible() {
        return isInterestingActivityVisible;
    }

    @OverridepublicvoidonActivityResumed(Activity activity) {
        if (activity instanceofMyInterestingActivity) {
             isInterestingActivityVisible = true;
        }
    }

    @OverridepublicvoidonActivityStopped(Activity activity) {
        if (activity instanceofMyInterestingActivity) {
             isInterestingActivityVisible = false;
        }
    }

    // Other state change callback stubs
    ....
}

Register your application class in AndroidManifest.xml:

  <application
        android:name="your.app.package.MyApplication"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >

Add onPause and onResume to every Activity in the project:

@OverrideprotectedvoidonResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@OverrideprotectedvoidonPause() {
  super.onPause();
  MyApplication.activityPaused();
}

In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.

Post a Comment for "Pageradapter Changed Adapters Content With Notifying"