Barcode
Barcode

Reputation: 970

how to close fragments to see activity below using OnNavigationItemSelectedListener

so the way my app is created is i have my MapsActivity which is loaded when the app starts, and i have a bottom navigation widget that has 3 icons and it's selected on the first icon by default (which i've named Map). the two other icons when selected are fragments.

I'm trying to close the fragment when i click on the Map icon in the navigation widget, since that is essentially my main activity.

this is what i have

now i'm stuck and can't get back to my MapsActivity because i don't know how to close the fragment

MapsActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)
    val mapFragment = supportFragmentManager
        .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)

    val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_navigation)
    bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

    getAutoCompleteSearchResults()
}

...

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
    when(item.itemId){
        R.id.nav_map -> {
            Log.d(TAG, "map pressed")
            // if there's a fragment, close it
            return@OnNavigationItemSelectedListener true
        }
        R.id.nav_A -> {
            Log.d(TAG, "Fragment A pressed")
            replaceFragment(FragmentA())
            return@OnNavigationItemSelectedListener true
        }
        R.id.nav_B -> {
            Log.d(TAG, "Fragment B pressed")
            replaceFragment(FragmentB())
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}

...

private fun replaceFragment(fragment: Fragment){
    val fragmentTransaction = supportFragmentManager.beginTransaction()

    fragmentTransaction.replace(R.id.fragmentContainer, fragment)
    fragmentTransaction.commit()
}

Upvotes: 0

Views: 150

Answers (1)

Dennis Murage
Dennis Murage

Reputation: 411

You can find the currently attached fragment and detach it from the activity:

private fun detachFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()

fragmentTransaction.detach(fragment)
fragmentTransaction.commit()
}
...

And have a method for checking the visible fragment:

private fun getVisibleFragment(): Fragment? {
val fragmentManager = MapsActivity.getSupportFragmentManager()
val fragments = fragmentManager.getFragments()
if(fragments != null) {
    for (fragment: Fragment in fragments) {
        if (fragment != null && fragment.isVisible()) {
            return fragment
        }
    return null
    }
}
...

Then use it inside your MainActivity:

...
when(item.itemId){
    R.id.nav_map -> {
        Log.d(TAG, "map pressed")
        // if there's a fragment, close it
        val visibleFragment = getVisibleFragment()
        if(visibleFragment != null && visibleFragment.getId() != R.id.map) {
            detachFragment(visibleFragment)
        }

        return@OnNavigationItemSelectedListener true
    }
...

Upvotes: 1

Related Questions