Last active
December 30, 2022 09:40
-
-
Save Zhuinden/49b1724bff1d2485971f1f872415703c to your computer and use it in GitHub Desktop.
Fragment multiple tabs without remove or replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private lateinit var swipeFragment: SwipeFragment | |
private lateinit var favoritesFragment: FavoritesFragment | |
private lateinit var newsFragment: NewsFragment | |
private val fragments: Array<out Fragment> get() = arrayOf(swipeFragment, favoritesFragment, newsFragment) | |
private fun selectFragment(selectedFragment: Fragment) { | |
var transaction = supportFragmentManager.beginTransaction() | |
fragments.forEachIndexed { index, fragment -> | |
if(selectedFragment == fragment) { | |
transaction = transaction.attach(fragment) | |
selectedIndex = index | |
} else { | |
transaction = transaction.detach(fragment) | |
} | |
} | |
transaction.commit() | |
} | |
private var selectedIndex = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
if (savedInstanceState == null) { | |
swipeFragment = SwipeFragment() | |
favoritesFragment = FavoritesFragment() | |
newsFragment = NewsFragment() | |
supportFragmentManager.beginTransaction() | |
.add(R.id.fragment_on_display, swipeFragment, "swipe") | |
.add(R.id.fragment_on_display, favoritesFragment, "favorites") | |
.add(R.id.fragment_on_display, newsFragment, "news") | |
.commitNow() | |
} else { | |
selectedIndex = savedInstanceState.getInt("selectedIndex", 0) | |
swipeFragment = supportFragmentManager.findFragmentByTag("swipe") as SwipeFragment | |
favoritesFragment = supportFragmentManager.findFragmentByTag("favorites") as FavoritesFragment | |
newsFragment = supportFragmentManager.findFragmentByTag("news") as NewsFragment | |
} | |
val selectedFragment = fragments[selectedIndex] | |
selectFragment(selectedFragment) | |
val homeButton = findViewById<ImageButton>(R.id.home_button) | |
homeButton.setOnClickListener{ | |
selectFragment(swipeFragment) | |
} | |
val listButton = findViewById<ImageButton>(R.id.list_button) | |
listButton.setOnClickListener { | |
selectFragment(favoritesFragment) | |
} | |
val newsButton = findViewById<ImageButton>(R.id.news_button) | |
newsButton.setOnClickListener { | |
selectFragment(newsFragment) | |
} | |
} | |
override fun onSaveInstanceState(bundle: Bundle) { | |
super.onSaveInstanceState(bundle) | |
bundle.putInt("selectedIndex", selectedIndex) | |
} | |
} |
@GibsonRuitiari i always use attach/detach in samples because otherwise people would complain that their LiveData observers still work even while the fragment is hidden, as the fragment being hidden doesn't make them onStop
'ped. This is because a Fragment can only be onStop
'd while also destroying its view.
If you need to make it faster because inflation is costly, then replace attach/detach
with show/hide
.
Apply Changes and restart activity:
Unable to destroy activity Can not perform this action after onSaveInstanceState
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick question, fragmentTransaction.detach() destroys the view hierarchy associated with the fragment so won't this be costly as opposed to just adding the fragments to the container and hiding/showing them when needed?