Don't hold static reference to fragment

This commit is contained in:
Tommy-Geenexus 2019-06-29 14:51:03 +02:00 committed by Daniel Micay
parent 4c36a7bb57
commit 6a03e84622
2 changed files with 17 additions and 18 deletions

View File

@ -418,7 +418,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
case R.id.action_view_document_properties: case R.id.action_view_document_properties:
DocumentPropertiesFragment DocumentPropertiesFragment
.getInstance((ArrayList<CharSequence>) mDocumentProperties) .newInstance(mDocumentProperties)
.show(getSupportFragmentManager(), DocumentPropertiesFragment.TAG); .show(getSupportFragmentManager(), DocumentPropertiesFragment.TAG);
return true; return true;

View File

@ -5,6 +5,7 @@ import android.app.Dialog;
import android.os.Bundle; import android.os.Bundle;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
@ -16,35 +17,33 @@ import org.grapheneos.pdfviewer.R;
public class DocumentPropertiesFragment extends DialogFragment { public class DocumentPropertiesFragment extends DialogFragment {
public static final String TAG = "DocumentPropertiesFragment"; public static final String TAG = "DocumentPropertiesFragment";
private static final String KEY_DOCUMENT_PROPERTIES = "key_document_properties"; private static final String KEY_DOCUMENT_PROPERTIES = "document_properties";
private static DocumentPropertiesFragment sDocumentPropertiesFragment;
private List<String> mDocumentProperties; private List<String> mDocumentProperties;
public static DocumentPropertiesFragment getInstance(final ArrayList<CharSequence> metaData) { public static DocumentPropertiesFragment newInstance(final List<CharSequence> metaData) {
if (sDocumentPropertiesFragment == null) { final DocumentPropertiesFragment fragment = new DocumentPropertiesFragment();
sDocumentPropertiesFragment = new DocumentPropertiesFragment();
final Bundle args = new Bundle(); final Bundle args = new Bundle();
args.putCharSequenceArrayList(KEY_DOCUMENT_PROPERTIES, metaData);
sDocumentPropertiesFragment.setArguments(args); args.putCharSequenceArrayList(KEY_DOCUMENT_PROPERTIES, (ArrayList<CharSequence>) metaData);
} else { fragment.setArguments(args);
final Bundle args = sDocumentPropertiesFragment.getArguments();
args.clear(); return fragment;
args.putCharSequenceArrayList(KEY_DOCUMENT_PROPERTIES, metaData);
}
return sDocumentPropertiesFragment;
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (getArguments() != null) {
mDocumentProperties = getArguments().getStringArrayList(KEY_DOCUMENT_PROPERTIES); mDocumentProperties = getArguments().getStringArrayList(KEY_DOCUMENT_PROPERTIES);
} }
}
@NonNull
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity(); final Activity activity = requireActivity();
final AlertDialog.Builder dialog = new AlertDialog.Builder(activity) final AlertDialog.Builder dialog = new AlertDialog.Builder(activity)
.setPositiveButton(android.R.string.ok, null); .setPositiveButton(android.R.string.ok, null);