1

I am working with nested fragment and I am facing issue when I press back.

Flow:

FragementActivity

A(Fragment)     B(Fragment)

 C      D        E     F

This is a structur of my application. Where C,D,E,F is child fragments and not added in back stack. D contains List fragment, and from D I called B. But When I press back from B I want again D with last selected position. I added A and B in back stack. How can I achieve this. Provide me some solutions. I recalled D but the selected position changed always.

Slk
  • 205
  • 5
  • 12
  • Possible duplicate of [Android 4.2: back stack behaviour with nested fragments](http://stackoverflow.com/questions/13418436/android-4-2-back-stack-behaviour-with-nested-fragments) – Blo Nov 07 '15 at 22:56
  • [Handle onBackPressed in Nested fragment ](https://stackoverflow.com/questions/26919303/android-how-to-handle-onbackpressed-nested-fragment) – razi May 23 '17 at 10:58

2 Answers2

0

Have you tried overriding the method : onBackPressed() ?

0

In addition to other functionality, you might have to call some functionality to hide the current visible fragment. I have attached snippet in Kotlin

override fun onBackPressed() {
    // ...

    val thisStep = myAdapter?.getItem(currentFragmentPosition) as MyFragmentStep

    thisStep.setInvisible(runData)
}


//This goes on your activity ^^^


fun setInvisible(runData: Bundle) {

    // ...

    view?.visibility = View.INVISIBLE
}


//This goes on your base class of fragments  ^^^
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
gpat
  • 1
  • 4