androidandroid-fragmentsandroid-actionbarandroid-navigationup-navigation

actionbar upnavigation from fragmentB to fragmentA


I'm trying to build with only fragments. The App opens with a Activity blank that only has a ActionBar upon clicking the hamburger icon it opens a drawer that give you a menu option.

Upon clicking on one of the Menu Items it opens the First Fragment which has a Recycler/Card View. Upon clicking one of the Cards it opens a new fragment with more details of the selected cards.

Now the problem is the detail fragment shows home icon cause I enable setDisplayHomeAsUpEnabled(true) but when I click on the back arrow it does not do anything. The hardware back button does take me back to the previous (Recycler/Card View) fragment. I also have setHasOptionsMenu(true) in the detail fragment. I put log tags everywhere to see when the home button reacts but nothing.

Hope someone can give me a hand.

Activity:

public class AppStart extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

ActionBarDrawerToggle actionBarDrawerToggle;
final String TAG = "AppSart: onBackPressed";
final String TAG1 = "AppSart: resetActionBar";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_start);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public void onBackPressed() {
    int stack = getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG,Integer.toString(stack));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;
}

public void resetActionBar(boolean childAction)
{
    Log.d(TAG1,Boolean.toString(childAction));
    if (childAction) {
        actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    } else {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
    }
}
}

Recycler Fragment:

public class ProductFragment extends Fragment {

final String TAG1 = "ProdFrag: onCreate";
final String TAG2 = "ProdFrag: onCreateView";
final String TAG3 = "ProdFrag: onResume";
final String TAG6 = "ProdFrag: ActionSetting";
final String TAG7 = "ProdFrag: home";

public ProductFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG1,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}

@Override
public void onResume() {
    super.onResume();
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG3,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_product,container,false);        
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG2,Integer.toString(stack));
    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.toolbar_menu, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    switch (item.getItemId()) {
        case R.id.action_settings:
            Log.d(TAG6,Integer.toString(stack));
            return true;
        default:
            break;
    }
    return false;
}
}

Detail Fragment:

public class ProductTabsFragment extends Fragment {

final String TAG1 = "TabFrag: onCreate";
final String TAG2 = "TabFrag: onResume";
final String TAG3 = "TabFrag: ActionSettings";
final String TAG4 = "TabFrag: home";
final String TAG5 = "TabFrag: onBckStkChng";
final String TAG6 = "TabFrag: onNavigateUp";

public ProductTabsFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG1,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}

@Override
public void onResume() {
    super.onResume();
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG2,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_product_tabs, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.toolbar_menu, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    switch (item.getItemId()) {
        case R.id.action_settings:
            Log.d(TAG3,Integer.toString(stack));
            return true;
        case android.R.id.home:
            Log.d(TAG4,Integer.toString(stack));
            //getActivity().onBackPressed();
            return true;
        default:
            break;
    }
    return false;
}

}

Also this is the code in the recycler adapter for when a card is click to load the detail Fragment:

cvProduct.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                ProductTabsFragment productTabsFragment = new ProductTabsFragment();
                AppCompatActivity activity = (AppCompatActivity) view.getContext();
                FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container,productTabsFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

Solution

  • For those who might have this problem. Turns out that when ActionBarDrawerToggle is used setToolbarNavigationClickListener needs to be used for the backarrow.

    The R.id.home in optionitemselected seems to be disabled. Hope this helps!

    so what I did was remove from the onOptionsItemSelected(MenuItem item):

    case android.R.id.home:
            Log.d(TAG4,Integer.toString(stack));
            return true;
    

    and modified resetActionBar(boolean childAction) in the main Activity which is called in the oncreate of fragmentA and fragmentB.

    public void resetActionBar(boolean childAction)
    {
        Log.d(TAG1,Boolean.toString(childAction));
        if (childAction) {
            actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Log.d(TAG4,"Clicked");
                    onBackPressed();
                }
            });
        } else {
            getSupportActionBar().setHomeButtonEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        }
    }