Status Update
Comments
[Deleted User] <[Deleted User]> #2
Are there many use cases for this? The current logic works fine with dark mode of platform, appcompat, and Compose.
[Deleted User] <[Deleted User]> #3
When I was trying out integrating this with Now in Android, I ran into this issue since Now in Android currently doesn't inherit from AppCompatActivity
and doesn't call AppCompatDelegate.setDefaultNightMode
, so maybe the solution is instead to inherit from AppCompatActivity
instead of just ComponentActivity
to update the underlying configuration.
jg...@google.com <jg...@google.com>
[Deleted User] <[Deleted User]> #4
I just tested it with ComponentActivity. It's working fine for me. The scrim for 3-button nav and the icons are properly changed on light/dark mode switch. The extension function detect light/dark mode from resources.configuration.uiMode
which is commonly used by the platform, AppCompat, and Compose, so I don't know why it doesn't work. Can I take a look at what you tried in Now in Android.
jg...@google.com <jg...@google.com> #5
The theme switcher based on the user's preference within the app is handled separately from Configuration.uiMode
and isn't updating the configuration directly, so I was attempting to call enableEdgeToEdge
and manually specifying the dark and light themes.
The two options I see:
- Allow a different way of specifying for
enableEdgeToEdge
whether the app is effectively in dark mode or light mode instead of that being based just onConfiguration.uiMode
(this issue) - Switch Now in Android to use
AppCompatActivity
andAppCompatDelegate.setDefaultNightMode
so that theConfiguration.uiMode
is updated based on the user's preference
jg...@google.com <jg...@google.com> #6
I see, so it has in-app light/dark setting.
On API 31+, apps with custom in-app light/dark settings should call UiModeManager.setApplicationNightMode
enableEdgeToEdge
as well.
On API <31, it's easy if you can use AppCompatDelegate.setDefaultNightMode(int)
So, custom mode is useful for apps that:
- Provide in-app light/dark mode settings on API <31.
- Don't want to depend on appcompat
I'm not sure if it's super common, but I guess the feature is worth implementing. I'll put it during the next alpha phase.
For Now in Android, I recommend you still call setApplicationNightMode
on API 31+ regardless of enableEdgeToEdge
. On API <31, just for the time being, you can either use the current e2e implementation, hide the setting, depend on appcompat, or just bear with the minor UI glitch. None of these are great, but I don't have a good solution now.
jg...@google.com <jg...@google.com> #7
Branch: androidx-main
commit 27e7d52e8604a080133e8b842db10c89b4482598
Author: Yuichi Araki <yaraki@google.com>
Date: Wed Apr 19 12:56:59 2023
Add SystemBarStyle.custom
Relnote: "Add an optional parameter `detectDarkMode` to SystemBarStyle.auto for detecting night mode by custom logic"
Test: androidx.activity.EdgeToEdgeTest.enableCustom
Bug: 278263793
Change-Id: I78322867ba3940939e137479383eca08efc9a2b6
M activity/activity/api/current.txt
M activity/activity/api/restricted_current.txt
M activity/activity/src/androidTest/java/androidx/activity/EdgeToEdgeTest.kt
M activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt
M activity/integration-tests/testapp/src/main/java/androidx/activity/integration/testapp/EdgeToEdgeActivity.kt
M activity/integration-tests/testapp/src/main/res/layout/edge_to_edge_activity.xml
jg...@google.com <jg...@google.com> #8
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.activity:activity:1.8.0-alpha06
[Deleted User] <[Deleted User]> #9
Currently the app that I am trying to make contains multiple Fragments with and without ViewPager2's accessible through a bottom navigation set of buttons. Through simple usage of moving to and from my ViewPager2 Fragments, I would see random instances of child Fragments disappearing from them.
[Deleted User] <[Deleted User]> #10
jg...@google.com <jg...@google.com> #11
Following on the logs above, I suspect that each instance of FragmentStateAdapter gets access to that one BaseFragment in FragmentStateAdapter::restoreState (restore state called on each stack entry).
Making sure each ViewPagerFragment instance only gets access to its own state (so the state is not shared between them) should eliminate this issue.
Not sure where the problem lies yet, whether in ViewPager2's interaction with the state saving stack, or in the sample app, so continuing to investigate.
[Deleted User] <[Deleted User]> #12
From the evidence above, I am pretty sure it should not affect ViewHolder pattern but never know. Would love to know what was happening though if you come across a solution.
jg...@google.com <jg...@google.com>
jg...@google.com <jg...@google.com> #13
- the same instance of ViewPagerFragment is being used for various back-stack entries
- multiple instances of FragmentStateAdapter are being created at each ViewPagerFragment view inflation, and each has their post-grace-period gcFragments() scheduled
- the same page Fragment instance reference is passed to all FragmentStateAdapter instances via restoreState, but can only be bound to one ViewHolder (the current visible one)
- once grace-period ends in non-visible FragmentStateAdapter instances, they see nothing is bound to their ViewHolders and garbage-collect the page Fragment
I will look further into the correct solution, potentially cancelling gcFragments() if a FragmentStateAdapter gets detached from a ViewPager2 (e.g. at ViewPagerFragment view destruction).
This would require a code, change, though, so for now, you can probably work around this by not re-using ViewPagerFragment instances, but creating a new one each time you add it to the stack. There might be a need for passing relevant state, e.g. current page.
Raw debugging notes below in case helpful. I reproduced the issue by switching multiple times between buttons 1 and 2.
***
# how many adapter instances exist
️$ cat ff2 | grep -E "FragmentStateAdapter@[^\}]+" -o | sort -u
FragmentStateAdapter@5,446
FragmentStateAdapter@5,474
FragmentStateAdapter@5,484
# how many adapter instances get their state restored
️$ cat ff2 | grep restoreState | grep -E "FragmentStateAdapter@[^\}]+" -o | sort -u
FragmentStateAdapter@5,474
FragmentStateAdapter@5,484
# where are Fragment pages created
️$ cat ff2 | grep createFragment -C1
{androidx.viewpager2.adapter.FragmentStateAdapter@5,446}.mFragments will be accessed at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:248)
Breakpoint reached at com.test.myapplication.VPAdapter.createFragment(ViewPagerFragment.kt:161)
{androidx.viewpager2.adapter.FragmentStateAdapter@5,446}.mFragments will be accessed at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:252)
# confirming that instance 446 is the only adapter creating the views; others are getting it from previously restored state
# for reference, FragmentStateAdapter.java:248 = "if (!mFragments.containsKey(itemId)) {"
# for reference, FragmentStateAdapter.java:252 = "mFragments.put(itemId, newFragment);"
️$ cat ff2 | grep ensureFragment | sort
{androidx.viewpager2.adapter.FragmentStateAdapter@5,446}.mFragments will be accessed at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:248)
{androidx.viewpager2.adapter.FragmentStateAdapter@5,446}.mFragments will be accessed at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:252)
{androidx.viewpager2.adapter.FragmentStateAdapter@5,474}.mFragments will be accessed at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:248)
{androidx.viewpager2.adapter.FragmentStateAdapter@5,484}.mFragments will be accessed at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:248)
# where is the state with a page Fragment reference coming from
Breakpoint reached
at androidx.viewpager2.adapter.FragmentStateAdapter.restoreState(FragmentStateAdapter.java:515)
at androidx.viewpager2.widget.ViewPager2.restorePendingState(ViewPager2.java:337)
at androidx.viewpager2.widget.ViewPager2.dispatchRestoreInstanceState(ViewPager2.java:362)
at android.view.View.restoreHierarchyState(View.java:17684)
at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:539)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:907)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Method.java:-1)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
# how many Fragment instances go through restoreViewState
️$ cat ff4 | grep "restoreViewState. fragment="
restoreViewState: fragment=ViewPagerFragment{d4490de (a6cfe7d3-5376-4015-8357-2408442116ee) id=0x7f070076 Frag1}
restoreViewState: fragment=BaseFragment{de52066 (f5afa133-40d7-45c1-8193-6ce2d4cb5489) f0}
restoreViewState: fragment=BaseFragment{de52066 (f5afa133-40d7-45c1-8193-6ce2d4cb5489) f0}
restoreViewState: fragment=ViewPagerFragment{d4490de (a6cfe7d3-5376-4015-8357-2408442116ee) id=0x7f070076 Frag1}
restoreViewState: fragment=BaseFragment{de52066 (f5afa133-40d7-45c1-8193-6ce2d4cb5489) f0}
restoreViewState: fragment=ViewPagerFragment{d4490de (a6cfe7d3-5376-4015-8357-2408442116ee) id=0x7f070076 Frag1}
# where are ViewPagerFragment instances created in the sample
```
var fragment: Fragment? = supportFragmentManager.findFragmentByTag("Frag$fragmentToOpen")
if(fragment == null) {
fragment = when(fragmentToOpen) {
1 -> ViewPagerFragment()
2 -> BlankFragment()
else -> throw IllegalArgumentException()
}
}
```
[Deleted User] <[Deleted User]> #14
Looking forward to erasing all of this ugly code on next update!
[Deleted User] <[Deleted User]> #15
binding.gamePagerRoot.apply {
// Set the Adapter for our Fragment
adapter = gamePagerAdapter
// Do not allow user touch for this ViewPager2 to affect its scroll, should only be scrolled programmatically
isUserInputEnabled = false
// Set the Page Transformation
setPageTransformer(genStackedPageTransformer2())
// Listen to page change calbacks
registerOnPageChangeCallback(onPageChangeCallback)
// Customize the RecyclerView child
val recyclerView = getChildAt(0) as RecyclerView
// Remove the overscroll shaddow
recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_NEVER
// Allow nested scrolling
recyclerView.isNestedScrollingEnabled = true
// If we are on a different page as specified by our ViewModel, make sure to scroll to the page instantly (no animation)
if(gamePagerVM?.gamePageIds?.size ?: 1 > 1) {
recyclerView.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener{
override fun onGlobalLayout() {
if(recyclerView.childCount >= gamePagerVM?.currPage ?: 0) {
viewTreeObserver.removeOnGlobalLayoutListener(this)
// If the current page is not 0, set the current page
setCurrentItem(gamePagerVM?.currPage ?: 0, false)
}
}
})
}
}
ap...@google.com <ap...@google.com> #16
Branch: androidx-master-dev
commit 7166ba571d594baa9016e02c3b5fe967da0b8ec0
Author: Jakub Gielzak <jgielzak@google.com>
Date: Tue Aug 27 16:20:54 2019
Fix for FragmentStateAdapter#gcFragments()
Fix for an issue with FragmentStateAdapter garbage collection mechanism.
The bug manifested itself in the following scenario:
- ViewPager2 host Fragment view recreated 3+ times in a window < 10s,
- Host Fragment instances added to the back stack,
- ViewPager2 adapter set to null in onViewDestoyed (too late as view
state already saved, but affecting PageFragment book-keeping).
The fix adds another criteria to gcFragments() in case a
PageFragment instance gets taken over by a later created instance of
FragmentStateAdapter.
Bug: 139095195
Test: ./gradlew viewpager2:connectedCheck
Change-Id: If42bc4e65e8fe52cc0dcd9bbf67a0c55d3c535b1
A viewpager2/src/androidTest/java/androidx/viewpager2/widget/HostFragmentBackStackTest.kt
M viewpager2/src/main/java/androidx/viewpager2/adapter/FragmentStateAdapter.java
ap...@google.com <ap...@google.com> #17
Branch: androidx-master-dev
commit 39daf73f75d04b1ba52b007781355595430f8dab
Author: Jakub Gielzak <jgielzak@google.com>
Date: Tue Aug 27 16:20:16 2019
Made test FragmentAdapter more flexible.
Needed for the next CL.
Bug: 139095195
Test: ./gradlew viewpager2:connectedCheck
Change-Id: I3aa4b8a131d3d7bca248f94641c5ee643c6f1819
M viewpager2/src/androidTest/java/androidx/viewpager2/widget/BaseTest.kt
M viewpager2/src/androidTest/java/androidx/viewpager2/widget/swipe/FragmentAdapter.kt
Description
Creating Sample app to verify, but in case this is already a known issue