Home > Back-end >  Up Arrow disabled on implementing App Bar with overflow menu
Up Arrow disabled on implementing App Bar with overflow menu

Time:12-03

I am developing a sport timer app and new to android programming. I have implemented overflow menu. I am facing two issues:

  1. Facing disappearing Up Arrow that is needed for navigation up in the hierarchy. Up arrow disappears as soon as one of the overflow menus is selected.
  2. As up arrow is missing, I pressed back button, but the screen turns empty showing just the title in App Bar.

I just checked various postings on this missing up arrow in SO and outside. But was not able to get any clue to fix this. I could not see any difference between the code that I have and the solutions given. And for empty screen no clue available anywhere at all.

I frequently come to SO for help and indeed got many. Requesting SOians to help me here. Thanks.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:visibility="visible"
    tools:context=".MainActivity">

    <TextView
        android:id="@ id/textView"
        android:layout_width="215dp"
        android:layout_height="48dp"
        android:background="@color/teal_200"
        android:text="@string/app_name"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@color/black"
        android:textSize="48sp"
        android:textStyle="bold"
        android:visibility="visible"
        app:layout_anchor="@ id/toolbar"
        app:layout_anchorGravity="center" />

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="69dp"
        android:background="@color/white"
        app:popupTheme="@style/Theme.PTimer.PopupOverlay" />


    <include
        android:id="@ id/include"
        layout="@layout/content_main"
        android:layout_height="match_parent"
        app:layout_anchor="@ id/include"
        app:layout_anchorGravity="center" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

The java code of main activity is below

package com.example.ptimer;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.ptimer.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setSupportActionBar(binding.toolbar);
 
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (id) {
            case R.id.action_settings:
                Fragment fragmentS = new  SettingsFragment();
                fragmentTransaction.replace(R.id.nav_host_fragment_content_main, fragmentS);
                fragmentTransaction.setReorderingAllowed(true);
                fragmentTransaction.addToBackStack(null);
                break;
            case R.id.action_kyc:
                Fragment fragmentK = new KnowYourCapacity_Fragment();
                fragmentTransaction.replace(R.id.nav_host_fragment_content_main, fragmentK);
                break;
            case R.id.action_about:
                Fragment fragmentA = new HelpFragment();
                Bundle bundle = new Bundle();
                bundle.putString("help_desc", getResources().getString(R.string.pBasics_desc));
                bundle.putString("help_image","basic");
                fragmentA.setArguments(bundle);
                fragmentTransaction.replace(R.id.nav_host_fragment_content_main, fragmentA);
                break;
            default:
                break;
        }

        fragmentTransaction.commit();
        
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        onBackPressed();
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

}

Blank screen and missing up arrow image of the emulator is given as a link below (not allowed to post the image as such by SO)

Edit 1: After moving getSupportActionBar().setDisplayHomeAsUpEnabled(true); statement to onCreateOptionsMenu UP arrow is being displayed. But clicking UP arrow button shows blank screen. My issue #2 still remains. Need help here. Thanks.

CodePudding user response:

I found solution for

issue 1: Adding getSupportActionBar().setDisplayHomeAsUpEnabled(true); in onCreateOptionsMenu issue 2: I came across a solution in https://medium.com/mobile-app-development-publication/learn-android-navigation-component-through-coding-79dc47b240b3 It worked perfectly well and solved white screen issue and also not using explicit method using fragment manager for traversing to another fragment. It was about linking menu items to navigation graph.

  • Related