Architecture Navigation Component : onCreateView gets called every time
Asked Answered
F

2

16

Everytime fragment instance is created and reloading when you press back.

How to overcome this issue?

Inability of having proper backstack in nav controller is a huge productivity issue. Hope it's a missing feature or a work-around made on purpose... IMO this is a must-have and has to be introduced sooner rather than later (however: https://issuetracker.google.com/issues/109856764 says We won't be allowing customization of the transaction type (such as hiding/showing, etc) for the foreseeable future.) :(

Nav-Framework is nice, however, it create fragment's view every time

Does anyone has solution?

My code is to navigate using Navigation

object NavigationHelper : Navigational {


override fun moveDownWithPop(view: View,id: Int,popStackId: Int) {
    Navigation.findNavController(view).navigate(id,
            null,
            NavOptions.Builder()
                    .setPopUpTo(popStackId,
                            true).build()
    )
}

override fun moveDownWithDataPop(view: View,id: Int,popStackId: Int,bundle : Bundle) {
    Navigation.findNavController(view).navigate(id,
            bundle,
            NavOptions.Builder()
                    .setPopUpTo(popStackId,
                            true).build()
    )
}

override fun moveUp(view: View) {
    Navigation.findNavController(view).navigateUp()
}

override fun moveDown(view: View,id: Int) {
    Navigation.findNavController(view).navigate(id)
}

override fun moveDown(view: View,id: Int,args : Bundle) {
    Navigation.findNavController(view).navigate(id,args)
}

fun navigateGraph(graphId : Int,context : Context){
    var finalHost: NavHostFragment?  = NavHostFragment.create(graphId)
    (context as MainActivity).supportFragmentManager.beginTransaction()
            .replace(com.admision.R.id.content,finalHost!!)
            .setPrimaryNavigationFragment(finalHost) // this is the equivalent to app:defaultNavHost="true"
            .commit()
}
}

i found some blog Why I Will Not Use Architecture Navigation Component

Flowering answered 26/2, 2019 at 6:27 Comment(0)
F
2

I did like this, but still I am waiting for resolution from google.

Here we did but still waiting for better approch

if (manageEventViewModel == null) {
    manageEventViewModel = ViewModelProviders.of(activity!!).get(ManageEventViewModel::class.java)
    manageEventViewModel!!.setBinder(binding!!,context!!,listingType)
    loadAdvertisement(binding!!.advertise.imgAdvertisement,binding!!.advertise.tvAdvertisement,binding!!.advertise.llAdvertisement)
}

Full code

class ManageEventsFragment : BaseFragment() {

private var binding: FragmentFindEventsBinding? = null
private var manageEventViewModel: ManageEventViewModel? = null
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
    if (binding == null)
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_find_events,container,false)


    var listingType: String? = null
    if (arguments != null) {
        listingType = arguments!!.getString(Constant.LISTINGTYPE)
        /*if (listingType != null && listingType.equals(Constant.SEARCHLIST)) {
            val data = arguments!!.getString(Constant.SEARCHEVENTMODEL)
            val managetEventDataModel = com.admision.manageevents.utils.Utils.getManageEventDataModel(data)
            manageEventViewModel!!.setSearchDataModel(managetEventDataModel!!)
        }*/
    }

    val sharedViewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)
    sharedViewModel!!.dataToShare.observe(this,Observer<ManageEventsDataModel> { managetEventDataModel ->
        listingType = Constant.SEARCHLIST
        manageEventViewModel!!.setSearchDataModel(managetEventDataModel!!)
    })

    if (manageEventViewModel == null) {
        manageEventViewModel = ViewModelProviders.of(activity!!).get(ManageEventViewModel::class.java)
        manageEventViewModel!!.setBinder(binding!!,context!!,listingType)
        loadAdvertisement(binding!!.advertise.imgAdvertisement,binding!!.advertise.tvAdvertisement,binding!!.advertise.llAdvertisement)
    }
    return binding!!.root
}
}
Flowering answered 12/3, 2019 at 6:31 Comment(2)
Thanks, it works, but as you said, we appreciate it if Google fixes itCarrion
Did you find any better approach?Tillman
C
0

like what Bhavesh did, in my case, I just had a list and just this line was enough for me in onCreateView:

if (adapter.isEmpty()) {
            viewModel.getShopCats()
}

and adapter just had to have this :

fun isEmpty(): Boolean {
        return modelList.isNullOrEmpty()
}
Carrion answered 29/6, 2019 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.