SetHomeButtonEnabled on toolbar in fragment

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

      

Hello,

I am using a new toolbar and showing in my fragment. However, I want to go back to the previous snippet by having setHomeButtonEnabled(true)

. However, there is no such function in my fragment in onCreateView. This works in Activity, but doesn't work in Fragment.

Is there anyway for the toolbar to display setHomeButtonEnabled so that the arrow is displayed so that the user can type 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 expanding AppCompatActivity

and usingappcompat-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 in the image, there is no arraw to the left of the toolbar for the user to type back. enter image description here

+3


source to share


2 answers


use

   appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

      



instead

   appCompatActivity.getActionBar().setDisplayHomeAsUpEnabled(true);

      

+6


source


If your fragment has ActionBar activated, that's good. For now, the navigationDrawer icon known as the hamburger icon, if you want to enable this you need the following method in your snippet.

primarily create instances.

private ActionBarDrawerToggle drawerToggle;
private DrawerLayout mDrawerLayout;

      

you need more examples.

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 that, your snippet should have some way to tell mainActivity when the drawer is closed or open, you can do this with the syncState () method.

    mDrawerLayout.post(new Runnable(){

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

    });

      

you can implement this integer as a separate method in the fragment and call from the main one.

Read the documentation here for more information. about the whole class

ActionBarDrawerToggle

+1


source







All Articles