简体   繁体   中英

What is the best way to work with android fragment backstack?

I am working with multiple fragments with only one Activity and I want to know how to manage fragment backstack on back press (the best way)

Any link or little explanation with some code can be helpful

All you need to make sure you call .addToBackStack(null) in Fragment Manager and Fragment Transaction

I'll suggest you to use Android Jetpack Navigation. A new and improved way to handle fragment transactions. This is way better than the old FragmentTransaction class method. It is highly recommended by Google itself. And I'm also learning it nowadays. There's a tutorial below.

Android Jetpack - Navigation

Example fragment backstack on back press:

FragmentsActivity is the activity which will be the container for all your fragments, so override FragmentsActivity's onBackPressed() like this -

override fun onBackPressed() {
        val fm = supportFragmentManager
        if (fm.backStackEntryCount > 0) { //if backstack contain any fragment than pop it
            fm.popBackStack()
        } else {                          // call super function normally 
            super.onBackPressed()
        }
    }

Now whenever launch a fragment like this -

//If you don't want it in backstack
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FragmentA())
.commit() 

//If you want it in backstack
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FragmentA())
.addToBackStack(null)           //null or any String TAG you want
.commit() 

Backpress on Fragments

Need your fragment to be notified when user presses back button?

The Dumb Way:

MyFragment.kt

requireActivity().onBackPressedDispatcher.addCallback(this,
    object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            // user pressed back button
        }
    })

The Rockstar Developer Way:

Add this dependency:

https://developer.android.com/reference/kotlin/androidx/activity/OnBackPressedDispatcher.html

implementation 'androidx.activity:activity-ktx:1.0.0-rc01'

requireActivity().onBackPressedDispatcher.addCallback(this) {
    // user pressed back button
}

The common answer for this query is to pop the backstack or navigate up to previous activity. What if you dont want to pop the backstack on a certain fragment?

Override your onbackpressed to handle these conditions:

override fun onBackPressed() {

    when(supportFragmentManager.fragments[0].javaClass.simpleName){
        "FragmentOne" -> doActionOne()
        "FragmentTwo" -> doActionTwo()
        ...
        else -> supportFragmentManager.popBackStack()
    }
}

this gets your name of your current fragment and then you decide what you would want to do. For example maybe for one of the fragments you want a dialog to ask "are you sure you want to leave?" ect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM