Fixed
Status Update
Comments
pe...@airbnb.com <pe...@airbnb.com> #2
Forgot to mention, the Pixel 1 is on Android 9.
il...@google.com <il...@google.com>
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 6f1170f62927702e491ac3aa7e55f68699d4c957
Author: jbwoods <jbwoods@google.com>
Date: Thu Jul 11 14:51:22 2019
Set mCurrentPrimaryItem to null it the current fragment is destroyed
In FragmentPagerAdapter and FragmentStatePagerAdapter if the current
Fragment is destroyed and mCurrentPrimaryItem is not set to null, it is
possible for the adapter to attempt to set the maxLifecycle and because
the Fragment has been removed an error is thrown.
When destroying items, we should check if the item being destroyed is
currently the primary item, and if it is, we should set
mCurrentPrimaryItem to null.
Test: tested in app
BUG: 137209870
Change-Id: I66e104160c9a15088dfc640e03a6afca021a5845
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentPagerAdapter.java
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentStatePagerAdapter.java
https://android-review.googlesource.com/1014732
https://goto.google.com/android-sha1/6f1170f62927702e491ac3aa7e55f68699d4c957
Branch: androidx-master-dev
commit 6f1170f62927702e491ac3aa7e55f68699d4c957
Author: jbwoods <jbwoods@google.com>
Date: Thu Jul 11 14:51:22 2019
Set mCurrentPrimaryItem to null it the current fragment is destroyed
In FragmentPagerAdapter and FragmentStatePagerAdapter if the current
Fragment is destroyed and mCurrentPrimaryItem is not set to null, it is
possible for the adapter to attempt to set the maxLifecycle and because
the Fragment has been removed an error is thrown.
When destroying items, we should check if the item being destroyed is
currently the primary item, and if it is, we should set
mCurrentPrimaryItem to null.
Test: tested in app
BUG: 137209870
Change-Id: I66e104160c9a15088dfc640e03a6afca021a5845
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentPagerAdapter.java
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentStatePagerAdapter.java
jb...@google.com <jb...@google.com> #4
This has been fixed internally and will available in the Fragment 1.1.0-rc02 release.
an...@google.com <an...@google.com> #5
da...@gmail.com <da...@gmail.com> #6
I also experienced this issue and seeing as it's marked as fixed I tried adding the fragment 1.1.0-SNAPSHOT, however it wasn't fixed there.
I tried the fragment 1.2.0-SNAPSHOT and it was fixed there.
So temp fix was to add the snapshots repo and manually add the fragment dependency:
repositories {
maven {
url 'https://ci.android.com/builds/submitted/5724286/androidx_snapshot/latest/repository/ '
}
}
dependencies {
implementation 'androidx.fragment:fragment:1.2.0-SNAPSHOT'
}
So my question is what's the timeline for a 1.1.0-rc02 release? And when will appcompat be updated reference this?
I tried the fragment 1.2.0-SNAPSHOT and it was fixed there.
So temp fix was to add the snapshots repo and manually add the fragment dependency:
repositories {
maven {
url '
}
}
dependencies {
implementation 'androidx.fragment:fragment:1.2.0-SNAPSHOT'
}
So my question is what's the timeline for a 1.1.0-rc02 release? And when will appcompat be updated reference this?
il...@google.com <il...@google.com> #7
Fragment 1.1.0-rc02 is out right now, actually.
AppCompat 1.1.0 stable will depend on Fragment 1.1.0 stable, which will include this change.
AppCompat 1.1.0 stable will depend on Fragment 1.1.0 stable, which will include this change.
ya...@gmail.com <ya...@gmail.com> #8
We are targeting sdk 28
I notice the release note for androidx.appcompat:appcompat:1.1.0-rc01 stated
Note: This version will only compile against the Q Beta 4 SDK
Will androidx.appcompat:appcompat:1.1.0-rc02, which contains this bug fix, have this limitation too?
Can an app targeting sdk 28 using upcoming androidx.appcompat:appcompat:1.1.0-rc02 ?
Thank you.
I notice the release note for androidx.appcompat:appcompat:1.1.0-rc01 stated
Note: This version will only compile against the Q Beta 4 SDK
Will androidx.appcompat:appcompat:1.1.0-rc02, which contains this bug fix, have this limitation too?
Can an app targeting sdk 28 using upcoming androidx.appcompat:appcompat:1.1.0-rc02 ?
Thank you.
il...@google.com <il...@google.com> #9
Re #8 - you'll need to *compile* against API 29 (that'll be true for 1.1.0-rc01 and all future versions of AppCompat), but there's no requirement to *target* API 29 - those are entirely different things as per https://medium.com/androiddevelopers/picking-your-compilesdkversion-minsdkversion-targetsdkversion-a098a0341ebd
Description
Version used: 1.1.0-rc01 (part of AppCompat 1.1.0-rc01)
Devices/Android versions reproduced on: API 28 x86 emulator and Pixel 1
When using the new `BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT` behavior on a `FragmentStatePagerAdapter`, along with `getItemPosition()` returning `POSITION_NONE`, the app crashes when calling `notifyDataSetChanged()` on the adapter. We are using this paradigm to replace all of the fragments in the adapter.
Here's what seems to be happening. All of the fragments are removed in a fragment transaction (`ViewPager` calls `FragmentStatePagerAdapter#finishUpdate()` once the fragments are removed). Then the new fragments are instantiated and added to a new fragment transaction. The `ViewPager` then calls `setPrimaryItem` which tries to set the max lifecycle on the `mCurrentPrimaryItem`. However, this `mCurrentPrimaryItem` is still referencing the old fragment that was removed in a separate transaction, which causes the crash. It seems like the `mCurrentPrimaryItem` should be nulled out if it is removed from the adapter.
I've attached a sample project and GIF. To reproduce, just launch the app and press the `Replace Fragments` action button. Here's the error stacktrace:
2019-07-10 20:03:16.876 32176-32176/com.example.android.horizontalpaging E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.horizontalpaging, PID: 32176
java.lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager FragmentManager{9187988 in HostCallbacks{ba35f21}}
at androidx.fragment.app.BackStackRecord.setMaxLifecycle(BackStackRecord.java:244)
at androidx.fragment.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:234)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1234)
at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:631)
at androidx.viewpager.widget.ViewPager.dataSetChanged(ViewPager.java:1086)
at androidx.viewpager.widget.ViewPager$PagerObserver.onChanged(ViewPager.java:3097)
at androidx.viewpager.widget.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:291)
at com.example.android.horizontalpaging.MainActivity.onOptionsItemSelected(MainActivity.java:67)
at android.app.Activity.onMenuItemSelected(Activity.java:3544)
at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:384)
at com.android.internal.policy.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1237)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:776)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:923)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:913)
at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:617)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:147)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)