Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setHomeButtonEnabled on toolbar in a fragment

Android studio 1.3
com.android.support:appcompat-v7:22.1.1

Hello,

I am using the new toolbar and displaying in my fragment. However, I want to be able to navigate back to the previous fragment by having the setHomeButtonEnabled(true). However, in my fragment in the onCreateView there is no such function. This works in the Activity, but doesn't work in the fragment.

Is there anyway to have the toolbar display the setHomeButtonEnabled so that an arrow is displayed so the user can nagivate back.

public class FileTransferFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_file_transfer, container, false);

        final Toolbar toolbar = (Toolbar)view.findViewById(R.id.app_bar);
        AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity();
        appCompatActivity.setSupportActionBar(toolbar);

    /* TRIED THIS BUT DIDN'T WORK */
        appCompatActivity.getActionBar().setHomeButtonEnabled(true);
        appCompatActivity.getActionBar().setDisplayHomeAsUpEnabled(true);
        return view;
    }
}

In my Activity I am extending the AppCompatActivity and using appcompat-v7:22.1.1

public class FileTransferActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_transfer);

        if(savedInstanceState == null) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_container,
                    FileTransferFragment.getFileTransferFragment(1234), "FileTransferFragment");
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.commit();
        }
    }
}

As you can see on the image there is no arraw on the left of the toolbar to allow the user to nagivate back. enter image description here

like image 772
ant2009 Avatar asked May 29 '15 11:05

ant2009


People also ask

How do I access the activity toolbar in fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.

How do I show fragments in activity XML?

You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

What object is used to add a fragment to a layout?

While creating a Fragment we must use onCreateView() callback to define the layout and in order to run a Fragment. Here the inflater parameter is a LayoutInflater used to inflate the layout, container parameter is the parent ViewGroup (from the activity's layout) in which our Fragment layout will be inserted.


2 Answers

use

   appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

instead of

   appCompatActivity.getActionBar().setDisplayHomeAsUpEnabled(true);
like image 78
kasto_manche Avatar answered Sep 21 '22 00:09

kasto_manche


If you have actionBar enabled in your fragment, its good thing. For now, navigationDrawer icon, known as hamburger icon, if want to enable that , you need the following method in your fragment.

first of all create instances.

private ActionBarDrawerToggle drawerToggle;
private DrawerLayout mDrawerLayout;

you need above instances.

drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            //Todo
            //you don't have to write here anything to enable icon
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            //Todo
            //you dont have to write here anything to enable icon
        }

    };
    mDrawerLayout.setDrawerListener(drawerToggle); 

after this, your fragment should have some way to inform the mainActivity when the drawer is closed or opened you can do that by syncState() method.

    mDrawerLayout.post(new Runnable(){

        @Override
        public void run(){
            //enable hamburger icon
            drawerToggle.syncState();
        }

    });

you can implement this whole as separate method in fragment and call from main.

Read documentation here to get more information. about the whole class

ActionBarDrawerToggle

like image 23
Ajay P. Prajapati Avatar answered Sep 19 '22 00:09

Ajay P. Prajapati