Have a fragment in which I display the recyclerView list.
When I click on a list item in MainActivity I call the method:
fun FragmentActivity?.replaceFragment(fragment: Fragment): Boolean {
if (this == null) return false
try {
supportFragmentManager?.beginTransaction()?.replace(
R.id.container, fragment,
fragment.createTagName()
)?.commit()
} catch (ignored: IllegalStateException) {
return false
}
return true
}
After this I press the system back button and I have a duplicate list.
Also i have in my MainActivity next fun:
override fun onBackPressed() {
val onBackPressListener = currentFragmentInContainer() as? OnBackPressListener
if (onBackPressListener?.onBackPress() != true) {
super.onBackPressed()
}
}
The issue is you have not added the current fragment to the back stack.
In order to get to the starting point, you have to mark the fragment. addToBackStack(tag:String) will help you to do that.
Code:
fun FragmentActivity?.replaceFragment(fragment: Fragment): Boolean {
if (this == null) return false
try {
supportFragmentManager?.beginTransaction()?.replace(
R.id.container, fragment,
fragment.createTagName()
).addToBackStack(fragment.createTagName())?.commit()
} catch (ignored: IllegalStateException) {
return false
}
return true
}
Here is the documentation about the method : addToBackStack
Related
im trying to have a button that will refresh the current Fragment with default values (i have checkboxes which, after checking all three, gets disabled.
I tried this, unfortunately after clicking the button, nothing happens.
private fun refreshFragment(){
binding.buttonRefresh.setOnClickListener {
reloadFragment()
}
}
private fun reloadFragment(){
context?.let {
val fragManager = (context as? AppCompatActivity)?.supportFragmentManager
fragManager?.let {
val currentFrag = fragManager.findFragmentById(R.id.mainContainer)
currentFrag?.let {
val fragTransaction = fragManager.beginTransaction()
fragTransaction.detach(currentFrag)
fragTransaction.attach(currentFrag)
fragTransaction.commit()
}
}
}
}
We have implemented bottom navigation as described here:
https://developer.android.com/guide/navigation/navigation-ui#bottom_navigation
https://medium.com/androiddevelopers/navigation-multiple-back-stacks-6c67ba41952f
We are using navigation version 2.4.1, which supports multiple backstacks out of the box. This saves fragment state so that in navigating from main fragment A -> B -> C -> B using the bottomnav, state of fragment B is saved and restored upon return. This is as intended and much requested behaviour.
However, for one of the fragments in our bottomnav menu, I would like the possibility to NOT save the state. This is due to some confusing behaviour when navigating using talkback. Is there a way in the navigation framework to set a flag to not save state for a single fragment? Or any other way to programmatically clear savedstate without actually doing so "manually" by resetting the UI elements in fragment onDestroy/onResume or similar?
What I did was just use the same androidx.navigation.ui.NavigationUI.setupWithNavController logic but change the saveState and other logic specific to my use case. You could apply this when navigating to one specific fragment.
this.findViewById<BottomNavigationView>(R.id.bottom_navigation).apply {
setOnItemSelectedListener { item ->
val builder = NavOptions.Builder().setLaunchSingleTop(true)
val destinationId = item.itemId
item.isChecked = true
if (
navController.currentDestination!!.parent!!.findNode(item.itemId)
is ActivityNavigator.Destination
) {
builder.setEnterAnim(R.anim.nav_default_enter_anim)
.setExitAnim(R.anim.nav_default_exit_anim)
.setPopEnterAnim(R.anim.nav_default_pop_enter_anim)
.setPopExitAnim(R.anim.nav_default_pop_exit_anim)
} else {
builder.setEnterAnim(R.animator.nav_default_enter_anim)
.setExitAnim(R.animator.nav_default_exit_anim)
.setPopEnterAnim(R.animator.nav_default_pop_enter_anim)
.setPopExitAnim(R.animator.nav_default_pop_exit_anim)
}
if (item.order and Menu.CATEGORY_SECONDARY == 0) {
builder.setPopUpTo(
navController.graph.findStartDestination().id,
inclusive = false,
saveState = false
)
}
val options = builder.build()
return#setOnItemSelectedListener try {
navController.navigate(destinationId, null, options)
// Return true only if the destination we've navigated to matches the MenuItem
(navController.currentDestination?.id ?: false) == destinationId
} catch (e: IllegalArgumentException) {
false
}
}
// Do nothing on reselect
setOnItemReselectedListener {}
val weakReference = WeakReference(this)
navController.addOnDestinationChangedListener(
object : NavController.OnDestinationChangedListener {
override fun onDestinationChanged(
controller: NavController,
destination: NavDestination,
arguments: Bundle?
) {
// Hide BottomNavigationView from top level fragments
if (topLevelDestinations.any { it == destination.id }) {
this#apply.visibility = View.VISIBLE
} else this#apply.visibility = View.GONE
// Highlight item in BottomNavigationView
val view = weakReference.get()
if (view == null) {
navController.removeOnDestinationChangedListener(this)
return
}
view.menu.forEach { item ->
if (destination.id == item.itemId) {
item.isChecked = true
}
}
}
})
}
On my action, I use the onBackPressed() method. When I compare the fragment that triggered the event, the id is never the same and always falls into the else-statement. What's wrong?
override fun onBackPressed() {
val navHost = this.supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val pressed = navHost?.childFragmentManager.fragments?.get(0) as IOnBackPressed
var currentFragment = navHost?.childFragmentManager.fragments?.get(0)
pressed?.onBackPressed()?.takeIf { it }?.let {
when (currentFragment.id) {
R.id.myfragmet -> {
// implementation
}
else -> {
super.onBackPressed()
}
}
}
}
I want to know from what fragment I returning when I press the back button, so that I can do the necessary action. Let me simplify the code:
override fun onBackPressed() {
// get the nav host in the action
val navHost = this.supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
// get the current fragment in the nav host
var currentFragment = navHost?.childFragmentManager.fragments?.get(0)
// Verify the fragment to do a action
when (currentFragment.id) {
R.id.myfragmet -> {
// implementation
}
else -> {
super.onBackPressed()
}
}
}
But the currentFragment.Id is never equals R.id.myfragemnt
I have a bottom sheet dialog fragment that i use as a menu for my bottom app bar.
If i click on the menu icon really quick two times, the dialog shows up two times and i have to close it two times which is annoying.
My code is as follows:
ActivityHome.kt
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
if(mBottomNavDrawerFragment != null && mBottomNavDrawerFragment!!.dialog!!.isShowing){
mBottomNavDrawerFragment?.dismiss()
return false
}
mBottomNavDrawerFragment = RoundedBottomSheetDialogFragment()
mBottomNavDrawerFragment?.show(supportFragmentManager, mBottomNavDrawerFragment?.tag)
true
}
R.id.BottomAppBar_fromHomeActivity_MenuMain_Search -> {
Toast.makeText(this, "Not Implemented yet!", Toast.LENGTH_SHORT).show()
false
}
else -> true
}
}
Can anyone help? Thanks
Show the dialogFragment using a tag. Check if the tag exists in the stack before showing it again
if(getChildFragmentManager().findFragmentByTag(FragmentDialog.TAG) == null) {
fragmentDialog.show(getChildFragmentManager(), FragmentDialog.TAG);
}
You can do a little bit hack here. Here is the code.
private var saveClickCounter = 0
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.home -> {
if (saveClickCounter++ == 0) {
//Your Dialog Showing Code
Handler().postDelayed({
saveClickCounter=0
},1000)
}
true
}
R.id.BottomAppBar_fromHomeActivity_MenuMain_Search -> {
Toast.makeText(this, "Not Implemented yet!", Toast.LENGTH_SHORT).show()
false
}
else -> true
}
}
Make a variable saveClickCounter to store your counts.
On clicks increase the value of saveClickCounter and change it to zero after N seconds of delayed. I used 1 second in below code.
I have a simple activity with a BottomNavigationView. I'm using fragments to implement the contents of the activity for the different pages.
When the user presses the back button, it's supposed to go back to the previously looked at page. The problem is, when you repeatedly switch back and forth between the pages (fragments), this entire history is recorded. Take this example:
A -> B -> A -> B -> C -> A -> C
Pressing the back button would result in the reverse, but instead I want this behaviour (I noticed it in the Instagram app):
C -> A -> B -> Exit App
So every fragment should only have one entry in the backstack. How do I do this? I do I remove the previous transactions for a fragment from the stack?
Is this at all possible using a FragmentManager? Or do I have to implement my own?
My Activity with the BottomNavigationView:
class ActivityOverview : AppCompatActivity() {
// Listener for BottomNavigationView
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
// "Home" menu item pressed
setActiveFragment(resources.getString(R.string.tag_fragment_home))
return#OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
// "Dashboard" menu item pressed
return#OnNavigationItemSelectedListener true
}
R.id.navigation_settings -> {
// "Settings" menu item pressed
setActiveFragment(resources.getString(R.string.tag_fragment_settings))
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_overview)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
navigation.menu.findItem(R.id.navigation_home).setChecked(true)
// Set initial fragment
setActiveFragment(resources.getString(R.string.tag_fragment_home))
}
override fun onBackPressed() {
// > 1 so initial fragment addition isn't removed from stack
if (fragmentManager.backStackEntryCount > 1) {
fragmentManager.popBackStack()
} else {
finish()
}
}
// Update displayed fragment
fun setActiveFragment(tag: String) {
val fragment = if (fragmentManager.findFragmentByTag(tag) != null) {
// Fragment is already initialized
if (fragmentManager.findFragmentByTag(tag).isVisible) {
// Fragment is visible already, don't add another transaction
null
} else {
// Fragment is not visible, add transaction
fragmentManager.findFragmentByTag(tag)
}
} else {
// Fragment is not initialized yet
when (tag) {
resources.getString(R.string.tag_fragment_home) -> FragmentHome()
resources.getString(R.string.tag_fragment_settings) -> FragmentSettings()
else -> null
}
}
if (fragment != null) {
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.container_fragment, fragment, tag)
transaction.addToBackStack(null)
transaction.commit()
}
}
}
At this point I'm pretty sure it doesn't work with FragmentManager, so I created a class to implement a stack that doesn't allow duplicates:
class NoDuplicateStack<T> {
val stack: MutableList<T> = mutableListOf()
val size: Int
get() = stack.size
// Push element onto the stack
fun push(p: T) {
val index = stack.indexOf(p)
if (index != -1) {
stack.removeAt(index)
}
stack.add(p)
}
// Pop upper element of stack
fun pop(): T? {
if (size > 0) {
return stack.removeAt(stack.size - 1)
} else {
return null
}
}
// Look at upper element of stack, don't pop it
fun peek(): T? {
if (size > 0) {
return stack[stack.size - 1]
} else {
return null
}
}
}
I then integrated this class into my activity:
class ActivityOverview : AppCompatActivity() {
val fragmentsStack = NoDuplicateStack<String>()
val fragmentHome = FragmentHome()
val fragmentSettings = FragmentSettings()
val fragmentHistory = FragmentHistory()
// Listener for BottomNavigationView
private val mOnNavigationItemSelectedListener = ...
override fun onCreate(savedInstanceState: Bundle?) {
...
}
override fun onBackPressed() {
if (fragmentsStack.size > 1) {
// Remove current fragment from stack
fragmentsStack.pop()
// Get previous fragment from stack and set it again
val newTag = fragmentsStack.pop()
if (newTag != null) {
setActiveFragment(newTag)
}
} else {
finish()
}
}
// Update displayed fragment
fun setActiveFragment(tag: String) {
val fragment = when (tag) {
resources.getString(R.string.tag_fragment_home) -> fragmentHome
resources.getString(R.string.tag_fragment_settings) -> fragmentSettings
resources.getString(R.string.tag_fragment_history) -> fragmentHistory
else -> null
}
if (fragment != null && !fragment.isVisible) {
fragmentManager.beginTransaction()
.replace(R.id.container_fragment, fragment, tag)
.commit()
fragmentsStack.push(tag)
}
}
}
I also faced the same problem, I did this which uses the system stack
val totalFragments = supportFragmentManager.backStackEntryCount
if (totalFragments != 0) {
val removed = supportFragmentManager.getBackStackEntryAt(totalFragments - 1)
poppedFragments.add(removed.name!!)
for (idx in totalFragments - 1 downTo 0) {
val fragment = supportFragmentManager.getBackStackEntryAt(idx)
if (!poppedFragments.contains(fragment.name)) {
supportFragmentManager.popBackStack(fragment.name, 0)
return
}
}
finish()
return
}
super.onBackPressed()
and then added this while launching the fragment
if (poppedFragments.contains(tag)) {
poppedFragments.remove(tag)
}