I'm using navigation controller.
I was wondering should I add an additional destination to go back? or there is a builtin call that does that.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_sign_up, container, false).apply {
val navController = NavHostFragment.findNavController(this#SignUpFragment)
this.btnSignUp.setOnClickListener {
navController.navigate(R.id.action_signUpFragment_to_homeActivity)
}
this.btnGotoLogin.setOnClickListener {
navController.navigate(R.id.action_signUpFragment_to_loginFragment)
}
this.back_to_auth_selection.setOnClickListener {
// should I add another action ?
}
}
}
this.back_to_auth_selection.setOnClickListener
{
activity?.onBackPressed()
}
You can also include an Up-button on each fragment other than the home fragment
To do this in main activity`
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// use nav controller to add Up-Button to the app
val navController = this.findNavController(R.id.nav_host_fragment)
// link the navigation controller to the app bar
NavigationUI.setupActionBarWithNavController(this, navController)
}
//override onSupportNavigateUp() to call navigateUp() in the navigation controller
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.nav_host_fragment)
return navController.navigateUp()
}
}`
Sorry my code is in Kotlin
Related
I have an app with 3 main destinations which can be accessed by a bottom nav view. Each destination has its own navigation graph.
The problem is that when I minimize and reopen my app, the navigation components reset to the default destination. Why does this happen?
My main activity: (Irrelevant code omitted)
class MainActivity : AppCompatActivity() {
// List of base host containers
val fragments = listOf(
HomeHostFragment(),
CoursesHostFragment(),
SearchHostFragment()
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val viewPager: ViewPager = findViewById(R.id.main_activity_pager)
viewPager.adapter = ViewPagerAdapter()
}
inner class ViewPagerAdapter : FragmentPagerAdapter(supportFragmentManager) {
override fun getItem(position: Int): Fragment =
fragments[position]
override fun getCount(): Int =
fragments.size
}
}
HomeHostFragment.kt:
class HomeHostFragment : Fragment() {
private lateinit var navController: NavController
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_home_host, container, false)
override fun onStart() {
super.onStart()
navController = requireActivity().findNavController(R.id.nav_host_home)
navController.setGraph(R.navigation.nav_graph_home)
NavigationUI.setupWithNavController(toolbar_home, navController)
}
fun onBackPressed(): Boolean {
return navController.navigateUp()
}
}
Whenever onStart() is called, NavigationUI.setupWithNavController() is called again which resets the navigation. Move this call to onViewCreated() so that the navigation setup is not done every time the fragment pauses and restarts.
I want to add my custom layout to the side of the existing Toolbar, keeping all features with NavigationUI (Up button, animations, placing up buttons and drawers, etc)
How can i achieve this?
After some research i`ve found out the solution
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val navController by lazy { findNavController(R.id.main_nav_host_fragment) }
private val appBarConfiguration by lazy { AppBarConfiguration(navController.graph) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = this.findNavController(R.id.main_nav_host_fragment)
NavigationUI.setupActionBarWithNavController(this, navController)
NavigationUI.setupWithNavController(
findViewById<NavigationView>(R.id.navigation_view),
navController
)
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.main_nav_host_fragment)
return NavigationUI.navigateUp(navController, appBarConfiguration)
}
}
FragmentToolbar.kt interface
interface FragmentToolbar {
fun clearActionBar()
fun setCustomActionBar()
}
MainFragment.kt
Next class MainFragment.kt is abstract with methods clearActionBar() and setCustomActionBar() that can be overridden. They are defined in FragmentToolbar.kt interface, because if you set custom layout in first fragment, you will see it also in all other fragments. So, you will almost always have to clear your ActionBar and this class is responsible for standard realisation. setCustomActionBar() is up to you\
abstract class MainFragment : Fragment(), FragmentToolbar {
val actionBar: ActionBar? by lazy { (requireActivity() as AppCompatActivity).supportActionBar }
override fun clearActionBar() {
actionBar?.apply {
setDisplayShowCustomEnabled(false)
setDisplayShowTitleEnabled(true)
}
}
override fun setCustomActionBar() {}
}
MyFragment.kt
class MyFragment : MainFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
clearActionBar() // defined in MainFragment
// Your code
}
override fun setCustomActionBar() {
actionBar?.apply {
setDisplayShowCustomEnabled(true)
setCustomView(R.layout.view_cart_and_bill)
}
actionBar?.customView?.apply { /* Here use findViewById to find necessary views inside your custom layout */ }
}
}
Hope my answer helps you. It also would be great, if ViewBinding could be used with this layout added in runtime, but i have not solved this issue yet
using the navigation component on two fragments, my app currently destroys a fragment when navigateUp() is called (using the setupActionBarWithNavController()), and therefore when I enter the deleted fragment, all progress is lost, I am trying to change that my adding the fragment to a backstack (and only one instance of the fragment) but I've been struggling with that...
Here's some code:
MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loginRepository = LoginRepository()
if (!loginRepository.checkLoggedInState()) {
goToLoginActivity()
}
Log.d("FirebaseMain", "onCreate: ${loginRepository.checkLoggedInState()}")
//MyApplication.app.currentUser
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
// setup custom action bar
setSupportActionBar(findViewById(R.id.toolbar))
// adds a return button in the ActionBar.
NavigationUI.setupActionBarWithNavController(this, navController)
}
FormFragment:
(the fragment I want to save its progress and add to backstack)
class FormFragment : Fragment() {
private lateinit var binding: FragmentFormBinding
private val checkListRecyclerAdapter = CheckListRecyclerAdapter(
hashMapOf(
0 to "mirrors",
1 to "blinkers1",
2 to "blinkers11",
3 to "blin4kers",
4 to "blink3e1123rs",
5 to "blink6ers",
6 to "blin53kers",
7 to "blin8kers",
8 to "blin7kers",
9 to "blin43kers",
10 to "blin32kers",
11 to "blin322kers",
)
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_form, container, false)
binding.rvCheckList.apply {
// this is used to load the data sequentially from top to bottom,
this.layoutManager = LinearLayoutManager(context)
// the adapter that loads the data into the list
this.adapter = checkListRecyclerAdapter
}
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
Thanks in advance for any help!
I've been struggling with some similar to this.
Fragments in Navigation Component doesnt keep their state. If you keep your data progress while navigate, you need create a SharedViewModel scoped to a Navigation Graph.
In Fragment:
private val navGraphScopedViewModel by navGraphViewModels<NavGraphViewModel>(R.id.your_nav_graph)
Create this class:
class NavGraphViewModel : ViewModel() {
var sharedData= CustomObject()
}
And from all your fragments you can access and set data:
val data = navGraphScopedViewModel.sharedData
I hope I've helped you
I want to show navigation drawer icon instead of Back button on a certain fragment. I created the App with Navigation graph.
Let's say i have 2 fragments (LoginFragment and DashboardFragment) and one activity (MainActivity)
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var drawerLayout: DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
drawerLayout = binding.drawerLayout
val navController = this.findNavController(R.id.myNavHostFragment)
// prevent nav gesture if not on start destination
navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
if (nd.id == nc.graph.startDestination) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
} else {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
}
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
NavigationUI.setupWithNavController(binding.navView, navController)
}
// Set up the back button on action bar
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.myNavHostFragment)
return NavigationUI.navigateUp(navController, drawerLayout)
}
}
LoginFragment.kt
class LoginFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding:FragmentLoginBinding = DataBindingUtil.inflate(inflater,
R.layout.fragment_login, container, false)
// Hide the Action bar
(activity as AppCompatActivity).supportActionBar?.hide()
binding.loginButton.setOnClickListener {
//Some unimportant validation
}
return binding.root
}
}
DashboardFragment.kt
class DashboardFragment : Fragment() {
lateinit var binding : FragmentDashboardBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Binding object for this fragment and the layout
binding = DataBindingUtil.inflate(inflater,
R.layout.fragment_dashboard, container, false)
//Navigate to Product stock fragment when clicked
binding.productStockButton.setOnClickListener(Navigation.createNavigateOnClickListener(
R.id.action_dashboardFragment_to_productStockOutletList
))
//Navigate to Switching History fragment when clicked
binding.switchingHistoryButton.setOnClickListener(Navigation.createNavigateOnClickListener(
R.id.action_dashboardFragment_to_switchingHistoryFragment
))
//Navigate to Outlet List fragment for Outstanding Product when clicked
binding.outstandingOrderButton.setOnClickListener(Navigation.createNavigateOnClickListener(
R.id.action_dashboardFragment_to_outletListFragment
))
// Set action bar title to "Main Dashboard"
(activity as AppCompatActivity).supportActionBar?.title = "Main Dashboard"
// Declare that this fragment has menu
setHasOptionsMenu(true)
(activity as AppCompatActivity).supportActionBar?.show()
(activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false)
//Return.... i don't know.
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater?.inflate(R.menu.nav_overflow_menu, menu)
}
}
And this is my Navigation graph (I set up LoginFragment as home)
I want to show the navigation drawer in the DashboardFragment instead of LoginFragment. (With LoginFragment still be the start of the graph). I already hide the Up button of the DashboardFragment
This is the current look of the dashboard. As you can see that the back button is already gone.
Is there anything i can do with it ? If there is something unclear let me know.
try this I hope that it will help you, this will hide actionBar on LoginFragment as it's the startDestination on graph and show it otherwise
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// prevent nav gesture if not on start destination
navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
if (nd.id == nc.graph.startDestination) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
supportActionBar?.hide()
} else {
supportActionBar?.show()
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}
}
// here this ids are form fragment which should show navigation icon
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home,R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
In my MainActivity.kt file, I can access supportActionBar.title and set the title.
In TitleScreenFragment.kt, I cannot access:
activity.supportActionBar.title
and when I try to just use activity.actionBar.title, that value is null.
How do I access activity.supportActionBar.title from within my fragment code?
MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,
R.layout.activity_main)
val navController = this.findNavController(R.id.nav_host_fragment)
NavigationUI.setupActionBarWithNavController(this, navController)
//THIS WORKS HOW DO I ACCESS THIS FUNCTIONALITY FROM FRAGMENT CODE???
supportActionBar?.title = "Ingredient Display"
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.nav_host_fragment)
return navController.navigateUp()
}
}
TitleScreenFragment.kt
class TitleScreenFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Setup the binding object with the layout and inflate it
val binding = DataBindingUtil.inflate<FragTitleBinding>(inflater, R.layout.frag_title, container, false)
// Set navigation action as an onclicklistener for button
binding.btnBeginInput.setOnClickListener{
view?.findNavController()?.navigate(R.id.action_titleScreenFragment_to_ingredientInputFragment)
}
//THIS IS NULL
println(activity?.actionBar?.title)
// return the inflated object in the binding
return binding.root
}
}
You can use something like:
(activity as AppCompatActivity).supportActionBar?.title = "Ingredient Display"
in the onViewCreated method (not in onCreateView).