Archived
BUGFIX: opening CanteenFragment.kt more than once would disallow refreshing from working properly
BUGFIX: fragments launch slowly when quickly clicking through the bottom navigation view
This commit is contained in:
@@ -3,7 +3,6 @@ package com.denizk0461.studip.activity
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import com.denizk0461.studip.R
|
||||
@@ -21,14 +20,12 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
// View binding
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
|
||||
/*
|
||||
* ID of the bottom navigation view button that launched the currently active fragment; set on
|
||||
* activity launch to the fragment that will be first shown
|
||||
* Tag for the currently active fragment; set on activity launch to the fragment that will be
|
||||
* shown first.
|
||||
*/
|
||||
private var currentFragment: Int = R.id.plan
|
||||
// All fragments are instantiated at app launch
|
||||
private val fragments: List<Fragment> =
|
||||
listOf(EventFragment(), CanteenFragment(), SettingsFragment())
|
||||
private var currentFragment: String = "event"
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
@@ -44,12 +41,15 @@ class MainActivity : AppCompatActivity() {
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
/*
|
||||
* Set up navigation view bar to launch fragments.
|
||||
* TODO: why do fragments launch slowly when quickly clicking between them?
|
||||
*/
|
||||
// Set up navigation view bar to launch fragments
|
||||
binding.contentMain.navView.setOnItemSelectedListener { item ->
|
||||
loadFragment(item.itemId)
|
||||
|
||||
// Launch fragment based on the item that has been clicked
|
||||
loadFragment(when (item.itemId) {
|
||||
R.id.food -> "canteen"
|
||||
R.id.settings -> "settings"
|
||||
else -> "event"
|
||||
})
|
||||
}
|
||||
|
||||
// Launch the fragment defined in currentFragment
|
||||
@@ -59,56 +59,38 @@ class MainActivity : AppCompatActivity() {
|
||||
/**
|
||||
* Loads a given fragment.
|
||||
*
|
||||
* @param id the ID of the bottom navigation view button that was clicked
|
||||
* @param type tag for the fragment that is to be opened
|
||||
* @return true on success, false if another fragment is currently instantiating
|
||||
*/
|
||||
private fun loadFragment(id: Int): Boolean {
|
||||
// Get fragment to be loaded
|
||||
val fragment = getFragment(id)
|
||||
private fun loadFragment(type: String): Boolean {
|
||||
|
||||
// Check if another fragment exists and is not fully initialised
|
||||
// Find the fragment that is to be opened, or create it if it hasn't been instantiated yet
|
||||
val fragment = supportFragmentManager.findFragmentByTag(type) ?: getFragment(type)
|
||||
|
||||
// Don't interrupt the current fragment's lifecycle if it isn't fully initialised
|
||||
if (fragment.lifecycle.currentState != Lifecycle.State.INITIALIZED) {
|
||||
// Return false to avoid interrupting the fragment's lifecycle
|
||||
return false
|
||||
}
|
||||
|
||||
// Prepare and commit a transaction between the current and the next fragment
|
||||
// Start the transaction to show the new fragment
|
||||
supportFragmentManager.beginTransaction()
|
||||
.replace(R.id.nav_host_fragment_content_main, fragment, id.toString())
|
||||
.replace(R.id.nav_host_fragment_content_main, fragment, type)
|
||||
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
|
||||
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
||||
.commit()
|
||||
|
||||
// Set text of the app bar accordingly
|
||||
// binding.contentMain.appTitleBar.text = getTitleString(id)
|
||||
// Note down the new current fragment
|
||||
currentFragment = id
|
||||
|
||||
// Transaction successful
|
||||
// Success
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve fragment by the ID of the bottom navigation view's button.
|
||||
* Retrieve fragment by its tag.
|
||||
*
|
||||
* @param id the ID of the bottom navigation button
|
||||
* @param type tag for the corresponding fragment
|
||||
* @return the corresponding fragment
|
||||
*/
|
||||
private fun getFragment(id: Int) = when (id) {
|
||||
R.id.food -> fragments[1] // CanteenFragment.kt
|
||||
R.id.settings -> fragments[2] // SettingsFragment.kt
|
||||
else -> fragments[0] // on unknown value or R.id.plan, launch EventFragment.kt
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve app bar text by the ID of the bottom navigation view's button.
|
||||
*
|
||||
* @param id the ID of the bottom navigation button
|
||||
* @return the corresponding fragment's title
|
||||
*/
|
||||
private fun getTitleString(id: Int) = when (id) {
|
||||
R.id.food -> getString(R.string.title_food)
|
||||
R.id.settings -> getString(R.string.title_settings)
|
||||
else -> getString(R.string.title_schedule) // on unknown value or R.id.plan, get text for EventFragment.kt
|
||||
private fun getFragment(type: String) = when (type) {
|
||||
"canteen" -> CanteenFragment() // CanteenFragment.kt
|
||||
"settings" -> SettingsFragment() // SettingsFragment.kt
|
||||
else -> EventFragment() // on unknown value or "event", launch EventFragment.kt
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user