Nested Fragments doesn't back to the previous one - kotlin

In main Activity I show new DialogFragment which is ParentFragment with only 1 frameLayout in it. From this fragment i add new Fragment into frame layout. (Fragment A)
val fragment = GetCheckQrChildFragment.newInstance()
childFragmentManager.beginTransaction()
.setPrimaryNavigationFragment(fragment)
.add(R.id.container, fragment)
.commit()
From the GetCheckQrChildFragment i can replace a fragment too (Fragment B)
val fragment = GetCheckChildFragment.newInstance()
fragmentManager!!.beginTransaction()
.setPrimaryNavigationFragment(fragment)
.replace(R.id.container,fragment)
.commit()
But if i press the back button from the last Fragment B it doesn't return me to Fragment A. It returns me to activity from i show Dialog Fragment. How can i return from Fragment B to Fragment A?

Related

Navigating from fragment A to fragment B and back to fragment A when i click back button in Kotlin

Hey guys i'll just get straight to the point. Here's my problem :
adapterProduk.onItemClick = {
val fragment = FragmentKeteranganProduk()
val transaction = fragmentManager?.beginTransaction()
transaction?.replace(R.id.layoutFragment,fragment)?.commit()
}
This code is already working perfectly and no error can be found, but what i want to know that if there's any solution to go back into starting point after the fragment is already changed when i click the back button? It's like intent finish() method but i don't want to "finish()" it yet. Any solution will be appreciated, thanks!

Navigation from Activity to previous location (previous tab(fragment) and other Activity)

I am using Kotlin. I faced the problem of the navigation from Activity to previous location. The ArtikelContent Activity can be open by 3 different location which are: 1.Home menu 2.Artikel home - at the "Semua" tab (Fragment Semua) 3. Artikel home - at the "KAT 1" tab (Fragment KAT 1). Im using imageview as a back button on Artikel Content. Im faced the problem to navigate to the previous location before i open the ArtikelContent Activity by using imageview as a button.
This is the code i use on the ArtikelContent.kt Activity for back navigation:
val btnback : ImageView = findViewById(R.id.buttonback)
btnback.setOnClickListener {
val intent = Intent (this, ArtikelHome::class.java)
startActivity(intent)
}
You are creating a new instance of ArtikelHome activity in this btnBack click listener because of the new Intent. If you want to navigate back to the ArtikelHome activity then use finish() like this:
btnback.setOnClickListener {
finish()
}
It will destroy the ArtikelContent activity and you will be navigated to the previous activity in stack which is ArtikelHome activity.

How to reference a button from different view in Fragment. Kotlin

I want to achieve a simple task. I have an invisible button in one layout and a have a Fragment. When this function is executed I want the button in the other layout to become visible. However this layout with the button is not in the Fragment layout, which means I have to reference that button in the Fragment, but I don't know how to go about that.
This is what Fragment will look like to first time users. The images you see are in a recyclerview which inflates a layout. That layout has the invisible button.
Fragment class
//item subscribed
if (subscribeValueFromPref) {
subscribeAbstract.visibility = View.GONE
// abstractDownload.visibility = View.VISIBLE
} else {
subscribeAbstract.visibility = View.VISIBLE
// abstractDownload.visibility = View.VISIBLE
}
}
When this line of code executed the button in the fragment is gone and the button from the other layout becomes visible. As you can see I put two strokes in front of that code. Once the code has been executed the layout should look like this.
Summary
I want to reference a button in another layout from a fragment class.
Do you even need to communicate between fragments? Your example looks like a single fragment with a subscribe button, and a RecyclerView that contains items which have a button that can be displayed or hidden. You can just make that part of your Adapter's state, like this:
class MyAdapter : RecyclerView.Adapter<ViewHolder>() {
var showDownloadButtons = false
set(value) {
field = value
// call this to update the display (calls onBindViewHolder for items)
notifyDataSetChanged()
}
override fun onBindViewHolder(viewHolder: ViewHolder, position.Int) {
...
// when displaying an item, show or hide the download button as appropriate
viewHolder.downloadButton.visibility = if (showDownloadButtons) VISIBLE else INVISIBLE
}
}
Then in your fragment, when you work out your UI state based on that subscription value, you can just handle your main button and tell the adapter what to display:
if (subscribeValueFromPref) {
subscribeAbstract.visibility = View.GONE
adapter.showDownloadButtons = true
} else {
subscribeAbstract.visibility = View.VISIBLE
adapter.showDownloadButtons = false
}

How to replace the fragment from an activity

I created a project in AS with bottom navigation, then added a recycler view to display some movies from TMDB API, now I want to change the fragment when I click on item.
I've implemented the method onClickItem, tried to change the fragment but the new fragment is added on top of the current fragment
before I click an item :
and after I click on one of the items:
Notice that on top of the Spider-Movie appears the text from the new fragment
This is how I "replace" the fragment (this function is inside HomeFragment:
popularMoviesAdapter.onItemClick = { movie ->
Log.d("onItemClick", movie.title)
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.nav_host_fragment_activity_main, MovieFragment())?.commit()
}
EDIT
This is my fragments component tree:
and I've changed the above .onClickItem with this:
var navController = view?.findNavController()
popularMoviesAdapter.onItemClick = { movie ->
var opt = HomeFragmentDirections.actionNavigationHomeToNavigationMovie()
navController?.navigate(opt)
}

How to refresh fragment by clicking on button

I want to refresh my fragment by clicking on its button. How to do it?
I try to do it like this but old version of fragment appears on the top of the screen (picture is attached):
binding.checkAnswersButton.setOnClickListener {
requireFragmentManager().beginTransaction().apply {
replace(R.id.quizFragment, QuizFragment())
.commit()
}
}
This will refresh the fragment :
val ft = parentFragmentManager.beginTransaction()
ft.detach(this).attach(this).commit()