|
|
|
@ -31,10 +31,8 @@ import androidx.fragment.app.DialogFragment;
|
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
|
|
import androidx.lifecycle.LifecycleObserver;
|
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
|
import androidx.lifecycle.LifecycleRegistry;
|
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
|
|
|
|
|
|
|
|
|
import static android.app.Activity.RESULT_CANCELED;
|
|
|
|
|
|
|
|
|
@ -42,12 +40,13 @@ public class FragmentDialogBase extends DialogFragment {
|
|
|
|
|
private boolean once = false;
|
|
|
|
|
private LifecycleOwner owner;
|
|
|
|
|
private LifecycleRegistry registry;
|
|
|
|
|
private Fragment targetFragment;
|
|
|
|
|
private String targetRequestKey;
|
|
|
|
|
private int targetRequestCode;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
owner = new LifecycleOwner() {
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
@ -57,9 +56,22 @@ public class FragmentDialogBase extends DialogFragment {
|
|
|
|
|
};
|
|
|
|
|
registry = new LifecycleRegistry(owner);
|
|
|
|
|
registry.setCurrentState(Lifecycle.State.CREATED);
|
|
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
|
targetRequestKey = savedInstanceState.getString("fair:key");
|
|
|
|
|
targetRequestCode = savedInstanceState.getInt("fair:code");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.i("Create " + this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
|
|
|
|
outState.putString("fair:key", targetRequestKey);
|
|
|
|
|
outState.putInt("fair:code", targetRequestCode);
|
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onResume() {
|
|
|
|
|
registry.setCurrentState(Lifecycle.State.RESUMED);
|
|
|
|
@ -132,28 +144,22 @@ public class FragmentDialogBase extends DialogFragment {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
public void setTargetFragment(@Nullable Fragment fragment, int requestCode) {
|
|
|
|
|
targetFragment = fragment;
|
|
|
|
|
targetRequestKey = fragment.getClass().getName();
|
|
|
|
|
targetRequestCode = requestCode;
|
|
|
|
|
Log.i("Set target " + this + " " + fragment + " request=" + requestCode);
|
|
|
|
|
|
|
|
|
|
fragment.getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
Log.i("Reset target " + FragmentDialogBase.this);
|
|
|
|
|
targetFragment = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void sendResult(int result) {
|
|
|
|
|
protected void sendResult(int resultCode) {
|
|
|
|
|
if (!once) {
|
|
|
|
|
once = true;
|
|
|
|
|
Log.i("Dialog target=" + targetFragment + " result=" + result);
|
|
|
|
|
if (targetFragment != null) {
|
|
|
|
|
Intent data = new Intent();
|
|
|
|
|
data.putExtra("args", getArguments());
|
|
|
|
|
targetFragment.onActivityResult(targetRequestCode, result, data);
|
|
|
|
|
Log.i("Dialog key=" + targetRequestKey + " result=" + resultCode);
|
|
|
|
|
if (targetRequestKey != null) {
|
|
|
|
|
Bundle args = getArguments();
|
|
|
|
|
args.putInt("requestCode", targetRequestCode);
|
|
|
|
|
args.putInt("resultCode", resultCode);
|
|
|
|
|
getParentFragmentManager().setFragmentResult(targetRequestKey, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|