使用溢出菜单实现 App Bar 时禁用向上箭头

Up Arrow disabled on implementing App Bar with overflow menu

我正在开发一个运动计时器应用程序,并且是 android 编程的新手。我已经实现了溢出菜单。我面临两个问题:

  1. 面对层次结构中向上导航所需的消失向上箭头。选择其中一个溢出菜单后,向上箭头就会消失。
  2. 由于缺少向上箭头,我按下了后退按钮,但屏幕变空,只显示应用栏中的标题。

我刚刚查看了 SO 和外部关于这个缺失的向上箭头的各种帖子。但是无法获得解决此问题的任何线索。我看不出我拥有的代码和给出的解决方案之间有任何区别。对于空白屏幕,根本没有任何线索。

我经常来SO求助,确实得到了很多。请求 SOians 在这里帮助我。谢谢。

<?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>

主要activity的java代码如下

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)

编辑 1:将 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 语句移动到 onCreateOptionsMenu 后显示向上箭头。但是单击向上箭头按钮显示空白屏幕。我的问题 #2 仍然存在。这里需要帮助。谢谢。

我找到了

的解决方案

问题 1:添加 getSupportActionBar().setDisplayHomeAsUpEnabled(true);在 onCreateOptionsMenu 中 问题 2: 我在 https://medium.com/mobile-app-development-publication/learn-android-navigation-component-through-coding-79dc47b240b3 中遇到了一个解决方案,它工作得很好,解决了白屏问题,也没有使用片段管理器的显式方法遍历到另一个片段。它是关于将菜单项链接到导航图。