mirror of https://github.com/M66B/FairEmail.git
Added result listener to dialogs
This commit is contained in:
parent
db6a5e3a59
commit
374fb5570a
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue