17

I am using Navigation Architecture in an image gallery, when I go from fragment A to B and then return back to A, these 3 methods are called again which will cause my gallery to reload, where I should load my data in fragment so when I come back from B to A my methods don't get called? :

  1. OnCreateView
  2. OnViewCreated
  3. OnResume

Step A to B

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210

5 Answers5

5

The trick is to not inflate the view again in onCreateView(). This will call all your lifecycle events again, but this is how you will be getting your fragment's state maintained.

This was suggested by Ian Lake from google's android team. Here is the reference.

var binding: FragmentFeedsBinding? = null

   override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    if (binding == null)
        binding = FragmentFeedsBinding.inflate(inflater, container, false)
    return binding?.root
}
Daniyal Javaid
  • 1,426
  • 2
  • 18
  • 32
1

navigate(...) is implemented using Fragment.instantiate(...) by FragmentNavigator. In the example provided, I would recommend calling popBackStack() to return to the previous fragment instead of navigate(...)

Cain Wong
  • 284
  • 1
  • 5
0

The fragment's lifecycle methods anyway will called again. You can google it how to work with fragment or activity lifecycle.

The main idea how to deal with lifecycle consists of saving view states, persisting data etc. If you are using fragments you can use retain fragments: just in your gallery fragment put the flag setRetainInstance(true) and the system won't call onCreate() and onDestroy() methods while you will rotate your phone , for example. Next step I recommend you to use AndroidArchitectureComponent which was introduced in 2017 You can read about it here.

In your case, I guess, you should create ViewModel for your fragment, load data in your ViewModel and put the data inside the LiveData object. Moreover, you should override onCreate in fragment and make the fragment retain (how to do it I've just describe earlier) and init your ViewModel here. After that, in onViewCreated or in onActivityCreated you need to observe your data via LiveData and ViewModel and just show it without reloading.

It's basic algorithm. The similar behavior might be achieved with different approaches like MVP, RxPM, MVVM (based on RxJava and RxAndroid) etc. I suppose that it is not the main purpose of my answer to describe all these patterns here. There are a lot of information in the internet (on StackOverflow too) just try to find it.

The main idea of it that you should load something in the object that can survive while the view destroys and just say to the view to get the available data from this object when the view will ready to do that.

aLT
  • 326
  • 3
  • 9
0

I am also facing the same problem where my fragment (Say Fragment A) is getting reloaded while coming back (via Device Back Button press) from other fragment (Fragment B). I have tried to find out the valid solution for this but not able to find the same. For the time being, I have done one temporary solution using Fragment lifecycle. When we popBackStack() a fragment (Fragment B), the other fragment (Fragment A) which is coming to foreground, its onCreateView() and onViewCreated() will be called. So I have managed the status using this lifecycle. When first time, our Fragment A will launch, its onAttach() function will run and in this I saved the fragment status as:

override fun onAttach(context: Context) {
    super.onAttach(context)
    fragmentStatus = "onAttach"
}

After in onViewCreated(), I changed this status to:

fragmentStatus = "onViewCreated"

And inside onViewCreated() I am checking for this status value as

if (fragmentStatus == "onAttach") {
        fragmentStatus = "onViewCreated"
//Write your code here for rest of the functionality
}

So now whenever this Fragment A will come into foreground, it will always found status as onViewCreated, until this fragment get destroyed and restarted.

Note: This is a temporary solution to avoid reloading of your work inside that Fragment, but system will restart that Fragment as per its requirement.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
-1

You can use ViewModel to retain the data and keep position of scroll. In Navigational component of Android the fragment's OnViewCreated and onCreateView is called each time some fragment navigates. Keep data in the ViewModel so that it does not gets destroyed