1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 09:47:13 +00:00

Forward compatible setTargetFragment

This commit is contained in:
M66B 2020-09-17 16:22:42 +02:00
parent 637e565fe0
commit 91eb8f04ef
2 changed files with 39 additions and 18 deletions

View file

@ -43,6 +43,7 @@ import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
@ -50,6 +51,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentResultListener;
import androidx.lifecycle.Lifecycle;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -217,6 +219,19 @@ public class FragmentBase extends Fragment {
public void onActivityCreated(Bundle savedInstanceState) {
Log.d("Activity " + this + " saved=" + (savedInstanceState != null));
super.onActivityCreated(savedInstanceState);
getParentFragmentManager().setFragmentResultListener(getClass().getName(), getViewLifecycleOwner(), 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);
}
});
scrollTo();
}

View file

@ -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);
}
}
}