0

I have manually/programmatically set up an up button in my toolbar for a fragment page with the following code in onCreateOptionsMenu in the fragment:

(activity as AppCompatActivity).setSupportActionBar?.setDisplayHomeAsUpEnabled(true)

Tapping the system back button will take the user back to the previous fragment but without an up button (that works) I think some users may get lost.

I am having difficulty trying to work out how to catch and handle the up button to pop the fragment off the back stack.

This SO post shows how to catch the the up button click however it is in Java and doesn't go on to explain how you would navigate up.

I think it needs to look something like the code below but there are errors everywhere:

  • The case android.R.id.home is showing an 'Incompatible types:Int and MenuItem' error?

  • onBackPressed() is showing an 'Unresolved reference' error.

Bad code:

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item) {
            android.R.id.home -> {
                onBackPressed()
                return true
            }
            else -> return super.onOptionsItemSelected(item)
        }
}

UPDATE:

A comment to another SO post has a potential solution in Kotlin. It catches the click on the up button and goes back to the previous fragment page but then the up button doesn't go away. So the up button now persists even on the top level fragment destinations in my app (the pages corresponding to each tab in the BottomNavigationView).

I think this might have to do with the fact that there is only one activity in my app and the way that I have set up the up button in the fragment as mentioned above? If so, is there a workaround or other way to set up the up button by referencing the fragment instead of the whole activity?


If it helps, this is the code in the RecyclerView inner ViewHolder class in the adapter.kt file that navigates to the fragment page in question:

class AdapterListItemDetails(val items: List<ItemsList>) : RecyclerView.Adapter<AdapterListItemDeatils.ItemsViewHolder>() {

    //overrides for OnCreateViewHolder, getItemCount, onBindViewHolder

    inner class ItemsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var currentItem: ItemsList? = null
        var currentPosition: Int = 0

        init {
             itemView.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.goto_details, null))
        }

        fun setData(itemsList: ItemsList, position: Int) {
            itemView.tview_Keys.text = itemsList!!.nameText

            this.currentItem = itemsList
            this.currentPosition = position
        }
    }
}
Dennis 815
  • 139
  • 1
  • 2
  • 12

2 Answers2

1

You have to override onBackPressed() method in activity and handle the fragment transactions with your manual code. If you could share some snippet of activity and fragment transactions will help me to give some proper solution.

Guru Karthi R
  • 324
  • 3
  • 14
  • I am using the Navigation architecture component which I understand should do all the hard work of handling all the fragment transactions. If it helps, this is the code in the `RecyclerView` inner `ViewHolder` class in the adapter.kt file that navigates to the fragment page in question: `init {itemView.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.goto_details, null))}` – Dennis 815 Jun 30 '18 at 14:16
0

Hi this is what i usually do: in an activity find the navController from your navHostFragment

val navController = this.findNavController(R.id.myNavHostFragment)

Make sure it's connected to the ActionBar

NavigationUI.setupActionBarWithNavController(this, navController)

Then simply override onSupportNavigateUp, find your navController then navigate up

override fun onSupportNavigateUp(): Boolean{
    val navController = this.findNavController(R.id.myNavHostFragment)
    return navController.navigateUp()
}
Doilio Matsinhe
  • 2,131
  • 16
  • 29