1

I have a menu inside the top actionbar that allows for changing a password, editing their profile, and logging out. What I need is, once they click the "edit bio" option, I want to add an arrow or a "<" to the actionbar on the left side to allow the user a "go back" option if they want to exit editing the profile as well as hiding the right side menu option while they are editing. Here's my code for inflating the menu on the right:

override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        inflater!!.inflate(R.menu.profiletoolbar, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    //get the actionbar selction when pressed
    override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
        R.id.userLogout -> {
            FirebaseAuth.getInstance().signOut()
            val i = Intent(context, Login::class.java)
            startActivity(i)
            true
        }
        R.id.editBio -> {
            editProfileBio()
            true
        }
        else -> super.onOptionsItemSelected(item)
    }

I really want to understand what the process is behind how this works. Can someone help me? I'd also be okay with just replacing the menu on the right with an arrow or a "<" as well, although I don't like that idea as much.

Jeff McBride
  • 73
  • 12

2 Answers2

0

How about this: setDisplayHomeAsUpEnabled()

R.id.editBio -> {
            editProfileBio()
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
            true
        }

To handle when pressed:

override fun onBackPressed() {
        super.onBackPressed()
        // do your stuff when pressed the back Button
    }

I'd also be okay with just replacing the menu on the right with an arrow or a "<" as well, although I don't like that idea as much.

Don't like that idea either. By enabling back Button when editbio clicked, you'll have access to the current menu and there will be a back Button too so this seems to be the perfect way to do that.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

You just have to listen for the R.id.home option

  1. Add a toolbar to activity layout.
  2. In your fragment declare that it needs to redraw the toolbar.

    setHasOptionsMenu(true);

  3. Set toolbar as action bar

    private fun setActionBar() {
        val toolbar = getActivity().findViewById(R.id.tb_main_toolbar)
    
        if (toolbar != null) {
            (getActivity() as AppCompatActivity).setSupportActionBar(toolbar)
        }
        val actionBar = (getActivity() as AppCompatActivity).supportActionBar
        if (null != actionBar) {
            customizeActionBar(actionBar)
        }
    }
    
    private fun customizeActionBar(actionBar: ActionBar) {
        actionBar.setDisplayHomeAsUpEnabled(true)
        actionBar.setTitle(R.string.logout)
        actionBar.setDisplayHomeAsUpEnabled(true)
        actionbar.setDisplayShowHomeEnabled(true)
        actionBar.setHomeAsUpIndicator(R.drawable.logout)
    }
    
  4. Override onOptionItemSelected and do your functionality

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home ->  
                FirebaseAuth.getInstance().signOut()
                val i = Intent(context, Login::class.java)
                startActivity(i)
                true
        }
        return true
    }
    
Jitendra A
  • 1,568
  • 10
  • 18
  • I tried implementing this and I get unresolved references on `setSupportActionBar` and `actionBar`. I didn't mention that this is inside a fragment if that makes a difference. Also I do have `lateinit var toolbar: ActionBar`. – Jeff McBride Sep 19 '18 at 06:28
  • I have updated the answer for fragment scenario please check – Jitendra A Sep 19 '18 at 07:00
  • What is `tb_main_toolbar`? Is that a menu layout item? – Jeff McBride Sep 19 '18 at 08:02
  • It is the id of the toolbar which is present in the layout file of activity – Jitendra A Sep 19 '18 at 08:54
  • I am getting this error: `This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.` – Jeff McBride Sep 21 '18 at 01:59
  • Ok, Use AppCompatActivity and for this issue you should see this answer https://stackoverflow.com/a/26515159/6393514 – Jitendra A Sep 21 '18 at 02:34