Added result listener to dialogs

This commit is contained in:
M66B 2020-09-18 19:49:38 +02:00
parent db6a5e3a59
commit 374fb5570a
1 changed files with 32 additions and 1 deletions

View File

@ -30,6 +30,7 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentResultListener;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry; import androidx.lifecycle.LifecycleRegistry;
@ -40,9 +41,18 @@ public class FragmentDialogBase extends DialogFragment {
private boolean once = false; private boolean once = false;
private LifecycleOwner owner; private LifecycleOwner owner;
private LifecycleRegistry registry; private LifecycleRegistry registry;
private String requestKey = null;
private String targetRequestKey; private String targetRequestKey;
private int targetRequestCode; private int targetRequestCode;
private static int requestSequence = 0;
public String getRequestKey() {
if (requestKey == null)
requestKey = getClass().getName() + "_" + (++requestSequence);
return requestKey;
}
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -58,15 +68,29 @@ public class FragmentDialogBase extends DialogFragment {
registry.setCurrentState(Lifecycle.State.CREATED); registry.setCurrentState(Lifecycle.State.CREATED);
if (savedInstanceState != null) { if (savedInstanceState != null) {
requestKey = savedInstanceState.getString("fair:request");
targetRequestKey = savedInstanceState.getString("fair:key"); targetRequestKey = savedInstanceState.getString("fair:key");
targetRequestCode = savedInstanceState.getInt("fair:code"); targetRequestCode = savedInstanceState.getInt("fair:code");
} }
getParentFragmentManager().setFragmentResultListener(getRequestKey(), this, new FragmentResultListener() {
@Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
int requestCode = result.getInt("requestCode");
int resultCode = result.getInt("resultCode");
Intent data = new Intent();
data.putExtra("args", result);
onActivityResult(requestCode, resultCode, data);
}
});
Log.i("Create " + this); Log.i("Create " + this);
} }
@Override @Override
public void onSaveInstanceState(@NonNull Bundle outState) { public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString("fair:request", requestKey);
outState.putString("fair:key", targetRequestKey); outState.putString("fair:key", targetRequestKey);
outState.putInt("fair:code", targetRequestCode); outState.putInt("fair:code", targetRequestCode);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -146,7 +170,14 @@ public class FragmentDialogBase extends DialogFragment {
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void setTargetFragment(@Nullable Fragment fragment, int requestCode) { public void setTargetFragment(@Nullable Fragment fragment, int requestCode) {
targetRequestKey = ((FragmentBase) fragment).getRequestKey(); if (fragment instanceof FragmentBase)
targetRequestKey = ((FragmentBase) fragment).getRequestKey();
else if (fragment instanceof FragmentDialogBase)
targetRequestKey = ((FragmentDialogBase) fragment).getRequestKey();
else {
Log.e("setTargetFragment=" + fragment.getClass().getName());
throw new IllegalArgumentException();
}
targetRequestCode = requestCode; targetRequestCode = requestCode;
Log.i("Set target " + this + " " + fragment + " request=" + requestCode); Log.i("Set target " + this + " " + fragment + " request=" + requestCode);
} }