Reputation: 63
I am using Google's recommended single activity pattern with Android Jetpack's Navigation component.
Fragment 1 is an authentication screen. After the user is authenticated and navigates to Fragment 2, I would like to make pressing of the Android back button close the app, and remove the back arrow in the app bar.
I have found methods, like onBackPressedDispatcher
, to add / remove functionality from the back press, but nothing that also removes the back arrow.
I have also tried app:popUpTo="@+id/firstFragment"
when navigating from Fragment 1 to Fragment 2, but that doesn't work either.
This should be possible to specify with a single line of code. Still trying to find. Any tips?
Upvotes: 4
Views: 14733
Reputation: 56
This is my code, you must setup appBarConfiguration manually. Like mine.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home,
R.id.nav_safety,
R.id.nav_myskin,
R.id.nav_setting
))
setupActionBarWithNavController(navController, appBarConfiguration)
Full code:
package limitless.android.uvindex.ui.activity
import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import android.view.Menu
import android.view.MenuItem
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import limitless.android.uvindex.R
import limitless.android.uvindex.databinding.ActivityMainBinding
import limitless.android.uvindex.utils.Utils
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
// Get the NavController
val navController = findNavController(R.id.nav_host_fragment_content_main)
binding.bnv.setupWithNavController(navController)
navController.graph = navController.navInflater.inflate(R.navigation.nav_graph)
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home,
R.id.nav_safety,
R.id.nav_myskin,
R.id.nav_setting
))
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_premium -> {
Utils.startActivity(this, Intent(this, PremiumActivity::class.java))
true
}
else -> super.onOptionsItemSelected(item)
}
}
}
The Resualt: Screenshot
Upvotes: 0
Reputation: 3320
You need to remove fragment1
from back-stack
when navigation to fragment2
fragment1
<fragment
android:id="@+id/fragment1"
android:name="packagenameforFragment1"
android:label="fragment1"
tools:layout="@layout/fragment_1" >
<action
android:id="@+id/action_Fragment1_to_Fragment2"
app:destination="@id/Fragment2_id"
app:launchSingleTop="true"
app:popUpTo="@+id/your_MainGraph_id"
app:popUpToInclusive="true" />
then when you navigate from fragment1 to fragment2 call this
findNavController(fragment).navigate(R.id.action_Fragment1_to_Fragment2)
to remove the back button from Fragment2 you can use this
in Activity onCreate()
val appBarConfiguration = AppBarConfiguration
.Builder(R.id.your_fragment2_id,R.id.any_other_ids_you_want)
.build()
then setup your toolbar like this
setupActionBarWithNavController(this, yourNavController, appBarConfiguration)
Upvotes: 23