mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Make hiding ActionBar up/back button in Final Fragment optional
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.briar.android.fragment;
|
package org.briarproject.briar.android.fragment;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.content.res.ColorStateList;
|
import android.content.res.ColorStateList;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@@ -16,6 +17,7 @@ import org.briarproject.briar.R;
|
|||||||
import androidx.activity.OnBackPressedCallback;
|
import androidx.activity.OnBackPressedCallback;
|
||||||
import androidx.annotation.ColorRes;
|
import androidx.annotation.ColorRes;
|
||||||
import androidx.annotation.DrawableRes;
|
import androidx.annotation.DrawableRes;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.StringRes;
|
import androidx.annotation.StringRes;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
@@ -93,11 +95,6 @@ public class FinalFragment extends Fragment {
|
|||||||
|
|
||||||
AppCompatActivity a = (AppCompatActivity) requireActivity();
|
AppCompatActivity a = (AppCompatActivity) requireActivity();
|
||||||
a.setTitle(args.getInt(ARG_TITLE));
|
a.setTitle(args.getInt(ARG_TITLE));
|
||||||
ActionBar actionBar = a.getSupportActionBar();
|
|
||||||
if (actionBar != null) {
|
|
||||||
actionBar.setDisplayHomeAsUpEnabled(false);
|
|
||||||
actionBar.setHomeButtonEnabled(false);
|
|
||||||
}
|
|
||||||
a.getOnBackPressedDispatcher().addCallback(
|
a.getOnBackPressedDispatcher().addCallback(
|
||||||
getViewLifecycleOwner(), new OnBackPressedCallback(true) {
|
getViewLifecycleOwner(), new OnBackPressedCallback(true) {
|
||||||
@Override
|
@Override
|
||||||
@@ -108,6 +105,18 @@ public class FinalFragment extends Fragment {
|
|||||||
return v;
|
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
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
@@ -115,6 +124,17 @@ public class FinalFragment extends Fragment {
|
|||||||
scrollView.post(() -> scrollView.fullScroll(FOCUS_DOWN));
|
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
|
* This is the action that the system back button
|
||||||
* and the button at the bottom will perform.
|
* and the button at the bottom will perform.
|
||||||
@@ -123,4 +143,13 @@ public class FinalFragment extends Fragment {
|
|||||||
requireActivity().supportFinishAfterTransition();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,4 +44,8 @@ public class ErrorFragment extends FinalFragment {
|
|||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
requireActivity().getSupportFragmentManager().popBackStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldHideActionBarBackButton() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user