1

I want to implement the up button in an android application with only one activity that changes its content with different fragment. I used the default navigation drawer activity provided by android studio where i added a frameLayout to the content_main. In the fragment where i want the up botton to be shown i added this line of code in the onCreateView method:

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

ad this line in the onCreate method:

setHasOptionsMenu(true);

and i added the method to catch the click of it:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Log.w("second fragment","clicked back");
        return true;
    }
    return super.onOptionsItemSelected(item);
}

in the activiy i set onCreateOptionsMenu like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

but the click of it isn't triggered. I tried to add a setting button and it is triggered. I already read a lot of question about this but i can't figure out how to resolve it

pool
  • 91
  • 1
  • 1
  • 8

3 Answers3

1

setHasOptionsMenu(true) should be called in method onCreate() to let the FragmentManager know that your fragment needs to receive options menu callbacks.

Weiyi
  • 1,843
  • 2
  • 22
  • 34
  • I put it bu i forgot to add in the question – pool Aug 09 '16 at 07:48
  • How do you implement `onCreateOptionsMenu`? – Weiyi Aug 09 '16 at 08:49
  • All things seem all right: you have called setHasOptionsMenu, you use android.R.id.home instead of R.id.home, onCreateOptionsMenu is OK. I have no idea. orz.... – Weiyi Aug 09 '16 at 13:11
  • already tried both and nor work.The strange thing is that even in the activity `onOptionsItemSelected` on `R.id.home` isn't triggered – pool Aug 09 '16 at 13:17
0

I believe you use this constructor for ActionBarDrawerToggle which takes a Toolbar as a parameter. If you are using this constructor, onOptionsItemSelected will not be called if you click on the indicator. There are questions/answers regarding this problem, for example:

AppCompat v7 Toolbar onOptionsItemSelected not called

But for me, none of them worked perfectly so I used the Toolbar-specific constructor and here it's a workaround for my case. I put a transparent view on top of the toolbar which is "visible" only when I show the back button and I handle the click myself (calling onBackPressed() in my case).

I know it's a kind of hack, but it needed and worked for me.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/main_background">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

            </android.support.design.widget.AppBarLayout>

            <FrameLayout
                android:id="@+id/layoutMainContent"
                android:layout_below="@id/appBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </RelativeLayout>

        <!-- The important view -->
        <View
            android:id="@+id/viewFakeBack"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:clickable="true"
            android:visibility="gone"/>

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>
Community
  • 1
  • 1
Tamás Cseh
  • 3,050
  • 1
  • 20
  • 30
  • Thank's, at the end i have found another workaround from various question, because also for me nor work – pool Sep 09 '16 at 13:25
-1

Try by using switch(...) case statement.

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    //this will make Hamburger button clickable.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    //this will make the HomeAsUpIndicator button clickable.
    switch (item.getItemId()) {
        case android.R.id.home:
           Log.w("second fragment","clicked back");
           break;
    }
    return super.onOptionsItemSelected(item);
}

Hope this will help you.

TheHound.developer
  • 396
  • 1
  • 3
  • 16