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:
DocumentPropertiesFragment
.getInstance((ArrayList<CharSequence>) mDocumentProperties)
.newInstance(mDocumentProperties)
.show(getSupportFragmentManager(), DocumentPropertiesFragment.TAG);
return true;

View File

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