3

I want to have a Backbutton in the default Actionbar on an new androidx jetpack navigation App.

I follow the google navigation-getting-started to build a navigation app, but I'm missing a Backbutton in the Actionbar so I add in the OnCreate of the MainActivity:

        ActionBar actionbar = getSupportActionBar();
        NavController nc = Navigation.findNavController(mainFragment.getView());
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(nc.getGraph()).build();
        androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(this,nc, appBarConfiguration); 

Now I see a Backbutton in the ActionBar if I'm not in the start main fragment, but there is no back function if I tap that button. The regular back-key works fine, but how do I get the actionbar backbutton to work.

enter image description here

Update: It is not working from second fragment to first(main/root) fragment. from third to second fragment it is working. nav-graph

Simon Featherstone
  • 1,716
  • 23
  • 40
Freudi
  • 136
  • 1
  • 10

3 Answers3

1

try this :-

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do your stuff here
            }
        });

second option is;-

@Override
    public boolean onSupportNavigateUp() {
        return super.onSupportNavigateUp();
        // do your stuff here
    }
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
  • leave comment if you have any query – Sandeep Malik Apr 17 '19 at 11:44
  • Second option is only called from 3 to 2 Fragment, not from 2 to first. – Freudi Apr 17 '19 at 11:59
  • flow the intent here for your activity where you have the fragments – Sandeep Malik Apr 17 '19 at 12:00
  • I dont know what you mean with intent in this case. Here ist the whole project: – Freudi Apr 17 '19 at 12:07
  • according to your code you can not do what you want to do. follow this link https://developer.android.com/guide/components/fragments and watch some tutorials on Fragment Transitions. Although I can help you here.... but this thing is very useful for upcoming days and you should be master in fragment concept.... learn it by doing yourself. for now don't take heavy task.... and please don't forget to appreciate the answer... best of luck – Sandeep Malik Apr 17 '19 at 12:18
  • I already read that, i don't why it is working without any of this just with the new nav_graph from fragment3 to 2 but not from 2 to 1. The imortant thing was adding the line with appBarConfiguration beacuse it is referencing the nav-graph. But why it isn't working on the last "home" stept to the root fragment? – Freudi Apr 17 '19 at 12:32
0
package com.pg.navigation;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;

import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements main_fragment.OnFragmentInteractionListener, Fragment2.OnFragmentInteractionListener, fragment3.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fragment mainFragment = getSupportFragmentManager().findFragmentById(R.id.fragment);
        ActionBar actionbar = getSupportActionBar();
        NavController nc = Navigation.findNavController(mainFragment.getView());
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(nc.getGraph()).build();
        androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(this,nc, appBarConfiguration);
    }

    @Override
    public boolean onSupportNavigateUp() {
        return super.onSupportNavigateUp();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }
}
Freudi
  • 136
  • 1
  • 10
0
   //Implement like this in your fragment

     class BackButtonWithMenu : Fragment() {
            private val menuProvider = object : MenuProvider {
                    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
                        menuInflater.inflate(R.menu.BackButton_frag_menu, menu)
                    }
            
                    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
                        return when (menuItem.itemId) {
                            R.id.termsBtn -> {
                                navigateToIntroductionPage()
                                false //check this
                            }
                            else -> false //check this
                        }
                    }
                }
            
            //set ActionBar to .show
            override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                    super.onViewCreated(view, savedInstanceState)
                    (requireActivity() as AppCompatActivity).supportActionBar?.show()

val menuHost: MenuHost = requireActivity()
            menuHost.addMenuProvider(menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
            
            }
        
        //Add these fun in your activity
        override fun onNavigateUp(): Boolean {
                return navController.navigateUp(appBarConfiguration)
            }
        
            override fun onSupportNavigateUp(): Boolean {
                onBackPressed()
                return true
            }
Sachin
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – abdo Salm Oct 05 '22 at 02:27