From b69eb30e9c3238ac6afcd0404cb663c1e555e7b4 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 10 Feb 2017 07:17:53 -0800 Subject: [PATCH] use the recommended pattern for DialogFragment construction Empty constructors only... --- .../eehouse/android/xw4/ConfirmThenAlert.java | 9 +++---- .../org/eehouse/android/xw4/DelegateBase.java | 2 ++ .../eehouse/android/xw4/DlgDelegateAlert.java | 25 ++++++++++++------- .../org/eehouse/android/xw4/DlgState.java | 15 +++++++++++ .../android/xw4/InviteChoicesAlert.java | 12 +++------ .../eehouse/android/xw4/NotAgainAlert.java | 13 +++------- .../org/eehouse/android/xw4/OkOnlyAlert.java | 12 +++------ .../eehouse/android/xw4/PrefsActivity.java | 8 ++++++ 8 files changed, 57 insertions(+), 39 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConfirmThenAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConfirmThenAlert.java index 83b9f6fa4..aea0e97df 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConfirmThenAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConfirmThenAlert.java @@ -30,19 +30,18 @@ public class ConfirmThenAlert extends DlgDelegateAlert { public static ConfirmThenAlert newInstance( DlgState state ) { - return new ConfirmThenAlert( state ); + ConfirmThenAlert result = new ConfirmThenAlert(); + result.addStateArgument( state ); + return result; } public ConfirmThenAlert() {} - public ConfirmThenAlert( DlgState state ) { super( state ); } @Override public Dialog onCreateDialog( Bundle sis ) { final Context context = getActivity(); - - getBundleData( sis ); - final DlgState state = getState(); + final DlgState state = getState( sis ); NotAgainView naView = (NotAgainView) LocUtils.inflate( context, R.layout.not_again_view ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java index 041d5caf3..c888af1a0 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java @@ -427,6 +427,8 @@ public class DelegateBase implements DlgClickNotify, { if ( m_activity instanceof MainActivity ) { ((MainActivity)m_activity).show( df ); + } else if ( m_activity instanceof PrefsActivity ) { + ((PrefsActivity)m_activity).show( df ); } else { Assert.assertTrue( !BuildConfig.DEBUG ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java index 1779e0066..8e533ba29 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java @@ -43,10 +43,24 @@ public class DlgDelegateAlert extends DialogFragment { private static final String STATE_KEY = "STATE_KEY"; private DlgState m_state; - public DlgDelegateAlert( DlgState state ) { m_state = state; } public DlgDelegateAlert() {} - protected final DlgState getState() { return m_state; } + protected final DlgState getState( Bundle sis ) + { + if ( null != sis ) { + m_state = (DlgState)sis.getParcelable( STATE_KEY ); + } else { + Bundle args = getArguments(); + Assert.assertNotNull( args ); + m_state = DlgState.fromBundle( args ); + } + return m_state; + } + + protected void addStateArgument( DlgState state ) + { + setArguments( state.toBundle() ); + } @Override public void onSaveInstanceState( Bundle bundle ) @@ -55,13 +69,6 @@ public class DlgDelegateAlert extends DialogFragment { bundle.putParcelable( STATE_KEY, m_state ); } - protected void getBundleData( Bundle sis ) - { - if ( null != sis ) { - m_state = (DlgState)sis.getParcelable( STATE_KEY ); - } - } - protected void checkNotAgainCheck( DlgState state, NotAgainView naView ) { if ( null != naView && naView.getChecked() ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgState.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgState.java index ce9b8d3a5..bdecb9620 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgState.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgState.java @@ -20,6 +20,7 @@ package org.eehouse.android.xw4; +import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; @@ -27,6 +28,8 @@ import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.ActionPair; public class DlgState implements Parcelable { + private static final String BUNDLE_KEY = "bk"; + public DlgID m_id; public String m_msg; public int m_posButton; @@ -67,6 +70,18 @@ public class DlgState implements Parcelable { return 0; } + public Bundle toBundle() + { + Bundle result = new Bundle(); + result.putParcelable( BUNDLE_KEY, this ); + return result; + } + + public static DlgState fromBundle( Bundle bundle ) + { + return (DlgState)bundle.getParcelable( BUNDLE_KEY ); + } + public void writeToParcel( Parcel out, int flags ) { out.writeInt( m_id.ordinal() ); out.writeInt( m_posButton ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteChoicesAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteChoicesAlert.java index ffb6aa28b..c74eb420d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteChoicesAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteChoicesAlert.java @@ -45,12 +45,9 @@ public class InviteChoicesAlert extends DlgDelegateAlert { public static InviteChoicesAlert newInstance( DlgState state ) { - return new InviteChoicesAlert( state ); - } - - public InviteChoicesAlert( DlgState state ) - { - super( state ); + InviteChoicesAlert result = new InviteChoicesAlert(); + result.addStateArgument( state ); + return result; } public InviteChoicesAlert() {} @@ -60,8 +57,7 @@ public class InviteChoicesAlert extends DlgDelegateAlert { { final Context context = getActivity(); - getBundleData( sis ); - final DlgState state = getState(); + final DlgState state = getState( sis ); final ArrayList means = new ArrayList(); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NotAgainAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NotAgainAlert.java index f65de710f..30665042a 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NotAgainAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NotAgainAlert.java @@ -39,12 +39,9 @@ public class NotAgainAlert extends DlgDelegateAlert { public static NotAgainAlert newInstance( DlgState state ) { - return new NotAgainAlert( state ); - } - - public NotAgainAlert( DlgState state ) - { - super( state ); + NotAgainAlert result = new NotAgainAlert(); + result.addStateArgument( state ); + return result; } public NotAgainAlert() {} @@ -54,8 +51,7 @@ public class NotAgainAlert extends DlgDelegateAlert { { final Context context = getActivity(); - getBundleData( sis ); - final DlgState state = getState(); + final DlgState state = getState( sis ); final NotAgainView naView = (NotAgainView) LocUtils.inflate( context, R.layout.not_again_view ); @@ -82,5 +78,4 @@ public class NotAgainAlert extends DlgDelegateAlert { Dialog dialog = builder.create(); return dialog; } - } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OkOnlyAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OkOnlyAlert.java index a6cdf8239..3a74feacf 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OkOnlyAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OkOnlyAlert.java @@ -39,12 +39,9 @@ public class OkOnlyAlert extends DlgDelegateAlert { public static OkOnlyAlert newInstance( DlgState state ) { - return new OkOnlyAlert( state ); - } - - public OkOnlyAlert( DlgState state ) - { - super( state ); + OkOnlyAlert result = new OkOnlyAlert(); + result.addStateArgument( state ); + return result; } public OkOnlyAlert() {} @@ -54,8 +51,7 @@ public class OkOnlyAlert extends DlgDelegateAlert { { final Context context = getActivity(); - getBundleData( sis ); - final DlgState state = getState(); + final DlgState state = getState( sis ); Dialog dialog = LocUtils.makeAlertBuilder( getActivity() ) .setTitle( state.m_titleId == 0 ? R.string.info_title : state.m_titleId ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsActivity.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsActivity.java index ab43095d7..a48544463 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsActivity.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsActivity.java @@ -24,6 +24,7 @@ import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.preference.PreferenceActivity; +import android.support.v4.app.DialogFragment; import junit.framework.Assert; @@ -153,4 +154,11 @@ public class PrefsActivity extends PreferenceActivity public void addFragment( XWFragment fragment, Bundle extras ) { Assert.fail(); } public void addFragmentForResult( XWFragment fragment, Bundle extras, RequestCode code ) { Assert.fail(); } + public void show( DialogFragment fragment ) + { + // This can't work right now because PrefsActivity doesn't inherit + // from anything that can show fragments. So I need to convert to the + // newer preferences framework too. :-( + Assert.fail(); + } }