Make hiding ActionBar up/back button in Final Fragment optional

This commit is contained in:
Torsten Grote
2022-02-16 10:37:27 -03:00
parent fb50a5ba45
commit 98dddf3572
2 changed files with 38 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
package org.briarproject.briar.android.fragment;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.LayoutInflater;
@@ -16,6 +17,7 @@ import org.briarproject.briar.R;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
@@ -93,11 +95,6 @@ public class FinalFragment extends Fragment {
AppCompatActivity a = (AppCompatActivity) requireActivity();
a.setTitle(args.getInt(ARG_TITLE));
ActionBar actionBar = a.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
}
a.getOnBackPressedDispatcher().addCallback(
getViewLifecycleOwner(), new OnBackPressedCallback(true) {
@Override
@@ -108,6 +105,18 @@ public class FinalFragment extends Fragment {
return v;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
// onAttach(Activity) is deprecated, we are told to cast the context
AppCompatActivity a = (AppCompatActivity) context;
ActionBar actionBar = a.getSupportActionBar();
if (shouldHideActionBarBackButton() && actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
}
}
@Override
public void onStart() {
super.onStart();
@@ -115,6 +124,17 @@ public class FinalFragment extends Fragment {
scrollView.post(() -> scrollView.fullScroll(FOCUS_DOWN));
}
@Override
public void onDetach() {
AppCompatActivity a = (AppCompatActivity) requireActivity();
ActionBar actionBar = a.getSupportActionBar();
if (shouldHideActionBarBackButton() && actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
super.onDetach();
}
/**
* This is the action that the system back button
* and the button at the bottom will perform.
@@ -123,4 +143,13 @@ public class FinalFragment extends Fragment {
requireActivity().supportFinishAfterTransition();
}
/**
* If you are overriding {@link #onBackButtonPressed()}
* and are no longer finishing the fragment, return false here.
* Otherwise the ActionBar back button will be missing in your activity.
*/
protected boolean shouldHideActionBarBackButton() {
return true;
}
}

View File

@@ -44,4 +44,8 @@ public class ErrorFragment extends FinalFragment {
requireActivity().getSupportFragmentManager().popBackStack();
}
@Override
protected boolean shouldHideActionBarBackButton() {
return false;
}
}