mirror of https://github.com/M66B/FairEmail.git
Lifecycle aware back listener
This commit is contained in:
parent
82dbd48e9c
commit
490c2b9a86
|
@ -35,10 +35,15 @@ import java.util.List;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private boolean contacts;
|
||||
private List<IBackPressedListener> backPressedListeners = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
|
@ -148,16 +153,19 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
|
|||
return findViewById(android.R.id.content);
|
||||
}
|
||||
|
||||
private List<IBackPressedListener> backPressedListeners = new ArrayList<>();
|
||||
void addBackPressedListener(final IBackPressedListener listener, LifecycleOwner owner) {
|
||||
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
public void onDestroyed() {
|
||||
Log.i("Removing back listener=" + listener);
|
||||
backPressedListeners.remove(listener);
|
||||
}
|
||||
});
|
||||
|
||||
public void addBackPressedListener(IBackPressedListener listener) {
|
||||
Log.i("Adding back listener=" + listener);
|
||||
backPressedListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeBackPressedListener(IBackPressedListener listener) {
|
||||
backPressedListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
for (IBackPressedListener listener : backPressedListeners)
|
||||
|
|
|
@ -148,4 +148,8 @@ public class FragmentBase extends Fragment {
|
|||
return false;
|
||||
return activity.hasPermission(name);
|
||||
}
|
||||
|
||||
void addBackPressedListener(ActivityBase.IBackPressedListener listener) {
|
||||
((ActivityBase) getActivity()).addBackPressedListener(listener, getViewLifecycleOwner());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -406,8 +406,7 @@ public class FragmentCompose extends FragmentBase {
|
|||
}
|
||||
});
|
||||
|
||||
((ActivityBase) getActivity()).addBackPressedListener(onBackPressedListener);
|
||||
|
||||
addBackPressedListener(onBackPressedListener);
|
||||
|
||||
// Initialize
|
||||
setSubtitle(R.string.title_compose);
|
||||
|
@ -613,8 +612,6 @@ public class FragmentCompose extends FragmentBase {
|
|||
if (pgpService != null)
|
||||
pgpService.unbindFromService();
|
||||
|
||||
((ActivityBase) getActivity()).removeBackPressedListener(onBackPressedListener);
|
||||
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
|
|
|
@ -511,7 +511,7 @@ public class FragmentMessages extends FragmentBase {
|
|||
}
|
||||
});
|
||||
|
||||
((ActivityBase) getActivity()).addBackPressedListener(onBackPressedListener);
|
||||
addBackPressedListener(onBackPressedListener);
|
||||
|
||||
// Initialize
|
||||
swipeRefresh.setEnabled(false);
|
||||
|
@ -1607,12 +1607,6 @@ public class FragmentMessages extends FragmentBase {
|
|||
}.execute(FragmentMessages.this, args, "messages:move");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
((ActivityBase) getActivity()).removeBackPressedListener(onBackPressedListener);
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
outState.putString("fair:searching", searching);
|
||||
|
|
Loading…
Reference in New Issue