UltimateBarX icon indicating copy to clipboard operation
UltimateBarX copied to clipboard

更新依赖androidx.appcompat:appcompat至1.3.0及以上会出现Fragment显示为空白

Open lmk26 opened this issue 4 years ago • 0 comments

更新依赖androidx.appcompat:appcompat至1.3.0及以上会出现非第一个Fragment都显示为空白。

复现方式

新建Activity File -> Activity ->Bottom Navigation Activity

修改Activity代码

public class FragmentActivity extends AppCompatActivity {

    private ActivityFragmentBinding binding;
    private int curFragmentIndex;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i("FragmentActivity onCreate");

        binding = ActivityFragmentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // 不用他的代码
//        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
//                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
//                .build();
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_fragment);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
//        NavigationUI.setupWithNavController(binding.navView, navController);

        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new DashboardFragment());
        fragments.add(new NotificationsFragment());
        for (int i = 0; i < fragments.size(); i++) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment, fragments.get(i), String.valueOf(i))
                    .setMaxLifecycle(fragments.get(i), Lifecycle.State.STARTED)
                    .hide(fragments.get(i))
                    .commit();
        }
        getSupportFragmentManager().beginTransaction()
                .show(fragments.get(curFragmentIndex = 0))
                .setMaxLifecycle(fragments.get(curFragmentIndex), Lifecycle.State.RESUMED)
                .commit();

        navView.setOnItemSelectedListener(item -> {
            select(item.getOrder());
            return true;
        });
    }

    public void select(int position) {
        getSupportFragmentManager().beginTransaction()
                .hide(fragments.get(curFragmentIndex))
                .show(fragments.get(position))
                .setMaxLifecycle(fragments.get(curFragmentIndex), Lifecycle.State.STARTED)
                .setMaxLifecycle(fragments.get(position), Lifecycle.State.RESUMED)
                .commit();
        curFragmentIndex = position;
    }
}

修改Activity xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

<!--    <fragment-->
<!--        android:id="@+id/nav_host_fragment_activity_fragment"-->
<!--        android:name="androidx.navigation.fragment.NavHostFragment"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="match_parent"-->
<!--        app:defaultNavHost="true"-->
<!--        app:layout_constraintBottom_toTopOf="@id/nav_view"-->
<!--        app:layout_constraintLeft_toLeftOf="parent"-->
<!--        app:layout_constraintRight_toRightOf="parent"-->
<!--        app:layout_constraintTop_toTopOf="parent"-->
<!--        app:navGraph="@navigation/mobile_navigation" />-->

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

修改bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:orderInCategory="0"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:orderInCategory="1"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:orderInCategory="2"
        android:title="@string/title_notifications" />
</menu>

在各个Fragment中添加

    @Override
    public void onResume() {
        super.onResume();
        UltimateBarX.statusBar(this)
                .light(true)
                .color(Color.WHITE)
                .apply();
    }

androidx.appcompat:appcompat版本为1.2.0或去掉Fragment中UltimateBarX的代码时显示正常。 看见有人说是升级Fragment的原因,appcompat从1.2.0升级到1.3.0也确实把Fragment1.1.0更新到1.3.4了。 https://developer.android.com/jetpack/androidx/releases/appcompat#version_130_3 image image image

lmk26 avatar Nov 26 '21 03:11 lmk26