fix FragmentAdapter returning null fragments when activity was recreated (#1647)
* fix FragmentAdapter returning null fragments when activity was recreated * remove unused import * make activity in CustomFragmentStateAdapter private
This commit is contained in:
parent
27d464351b
commit
45828fbb5a
@ -21,33 +21,23 @@ import com.keylesspalace.tusky.fragment.AccountMediaFragment
|
||||
import com.keylesspalace.tusky.fragment.TimelineFragment
|
||||
import com.keylesspalace.tusky.interfaces.RefreshableFragment
|
||||
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import java.lang.ref.WeakReference
|
||||
import com.keylesspalace.tusky.util.CustomFragmentStateAdapter
|
||||
|
||||
class AccountPagerAdapter(
|
||||
activity: FragmentActivity,
|
||||
private val accountId: String
|
||||
) : FragmentStateAdapter(activity) {
|
||||
|
||||
private val fragments = MutableList<WeakReference<Fragment>?>(TAB_COUNT) { null }
|
||||
) : CustomFragmentStateAdapter(activity) {
|
||||
|
||||
override fun getItemCount() = TAB_COUNT
|
||||
|
||||
override fun createFragment(position: Int): Fragment {
|
||||
val fragment: Fragment = when (position) {
|
||||
return when (position) {
|
||||
0 -> TimelineFragment.newInstance(TimelineFragment.Kind.USER, accountId, false)
|
||||
1 -> TimelineFragment.newInstance(TimelineFragment.Kind.USER_WITH_REPLIES, accountId, false)
|
||||
2 -> TimelineFragment.newInstance(TimelineFragment.Kind.USER_PINNED, accountId, false)
|
||||
3 -> AccountMediaFragment.newInstance(accountId, false)
|
||||
else -> throw AssertionError("Page $position is out of AccountPagerAdapter bounds")
|
||||
}
|
||||
|
||||
fragments[position] = WeakReference(fragment)
|
||||
return fragment
|
||||
}
|
||||
|
||||
fun getFragment(position: Int): Fragment? {
|
||||
return fragments[position]?.get()
|
||||
}
|
||||
|
||||
fun refreshContent() {
|
||||
|
@ -17,21 +17,16 @@ package com.keylesspalace.tusky.pager
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import com.keylesspalace.tusky.TabData
|
||||
import java.lang.ref.WeakReference
|
||||
import com.keylesspalace.tusky.util.CustomFragmentStateAdapter
|
||||
|
||||
class MainPagerAdapter(val tabs: List<TabData>, activity: FragmentActivity) : FragmentStateAdapter(activity) {
|
||||
private val fragments = MutableList<WeakReference<Fragment>?>(tabs.size) { null }
|
||||
class MainPagerAdapter(val tabs: List<TabData>, activity: FragmentActivity) : CustomFragmentStateAdapter(activity) {
|
||||
|
||||
override fun createFragment(position: Int): Fragment {
|
||||
val tab = tabs[position]
|
||||
val fragment = tab.fragment(tab.arguments)
|
||||
fragments[position] = WeakReference(fragment)
|
||||
return fragment
|
||||
return tab.fragment(tab.arguments)
|
||||
}
|
||||
|
||||
override fun getItemCount() = tabs.size
|
||||
|
||||
fun getFragment(position: Int): Fragment? = fragments[position]?.get()
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
/* Copyright 2019 Tusky Contributors
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
package com.keylesspalace.tusky.util
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
|
||||
abstract class CustomFragmentStateAdapter(
|
||||
private val activity: FragmentActivity
|
||||
): FragmentStateAdapter(activity) {
|
||||
|
||||
fun getFragment(position: Int): Fragment?
|
||||
= activity.supportFragmentManager.findFragmentByTag("f" + getItemId(position))
|
||||
}
|
Loading…
Reference in New Issue
Block a user