在某些 Fragments 中正确隐藏 TabLayout 和 Toolbar / Handling onPause/onResume

Hide TabLayout and Toolbar in some Fragments / Handling onPause/onResume correctly

我正在使用一种带有导航图的 Activity 方法。 我不希望我的选项卡布局或工具栏在某些片段中可见。我写了一个超类来扩展那些我不想要的片段。但不知何故,我无法正确理解生命周期,因为这不起作用:

 public class NoTabLayoutFragment extends Fragment {

    @Override
    public void onResume() {
        super.onResume();
        hideTabLayout();

    }

    @Override
    public void onPause() {
        super.onPause();
        showTabLayout();
    }

    private void showTabLayout() {
        if (getActivity() != null && getActivity().findViewById(R.id.fragment_main_tab_layout) != null) {
            getActivity().findViewById(R.id.fragment_main_tab_layout).setVisibility(View.VISIBLE);
        }

    }

    private void hideTabLayout() {
        if (getActivity() != null && getActivity().findViewById(R.id.fragment_main_tab_layout) != null) {
            getActivity().findViewById(R.id.fragment_main_tab_layout).setVisibility(View.GONE);
        }

    }
}  

public class NoBarsFragment extends NoTabLayoutFragment {
    @Override
    public void onResume() {
        super.onResume();
        hideToolbar();
    }

    @Override
    public void onPause() {
        super.onPause();
        showToolbar();
    }

    private void hideToolbar() {
        if (getActivity() != null && ((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
        }
    }

    private void showToolbar() {
        if (getActivity() != null && ((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
            ((AppCompatActivity) getActivity()).getSupportActionBar().show();
        }
    }
}

我做错了什么?

我选择另一种方法:

创建:

public interface HideBehaviourListener {

onHide();
onRestore();

}
public interface ToolbarHideListener extends HideBehaviourListener {

}
public interface TabLayoutHideListener extends HideBehaviourListener {

}

现在,在您的 Fragment 中将添加:

public class SomeFragment {

private ToolbarHideListener toolbarListener;
private TabLayoutHideListener tabLayoutListener;

@Override
public void onAttach(final Context context) {
super.onAttach(context);
toolbarListener = (ToolbarHideListener) context;
tabLayoutListener = (TabLayoutHideListener) context;
}

public void onResume() {
toolbarListener.onHide();
tabLayoutListener.onHide();
}

public void onPause() {
toolbarListener.onRestore();
tabLayoutListener.onRestore();
}
}

所以...在您的 Activity 中需要实现与您的逻辑接口。使用这种方法,您的片段不知道 Activity 的任何资源或行为。

根据 Listen for Navigation Events documentation,更改 UI 可见性的正确方法是通过 OnDestinationChangedListener:

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
    @Override
    public void onDestinationChanged(@NonNull NavController controller,
           @NonNull NavDestination destination, @Nullable Bundle arguments) {
       if(destination.getId() == R.id.full_screen_destination) {
           getSupportActionBar().hide()
       } else {
           getSupportActionBar().show()
       }
   }
});