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
OnCreateView is called twice without pressing back button or on any other event. Its called twice, once when fragment initially creates and again just after findNavController. navigate event.
To take full advantage of the Navigation component, your app should use multiple fragments in a single activity. However, activities can still benefit from the Navigation component. Note, however, that your app's UI must be visually broken up across several navigation graphs.
Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.
Among other goodies, Android Jetpack comes with the Navigation architecture component, which simplifies the implementation of navigation in Android apps. In this tutorial, you'll add navigation to a simple book-searching app and learn about: Navigation graphs and nested graphs. Actions and destinations.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With