use the recommended pattern for DialogFragment construction

Empty constructors only...
This commit is contained in:
Eric House 2017-02-10 07:17:53 -08:00
parent 8c92f2012e
commit b69eb30e9c
8 changed files with 57 additions and 39 deletions

View file

@ -30,19 +30,18 @@ public class ConfirmThenAlert extends DlgDelegateAlert {
public static ConfirmThenAlert newInstance( DlgState state ) public static ConfirmThenAlert newInstance( DlgState state )
{ {
return new ConfirmThenAlert( state ); ConfirmThenAlert result = new ConfirmThenAlert();
result.addStateArgument( state );
return result;
} }
public ConfirmThenAlert() {} public ConfirmThenAlert() {}
public ConfirmThenAlert( DlgState state ) { super( state ); }
@Override @Override
public Dialog onCreateDialog( Bundle sis ) public Dialog onCreateDialog( Bundle sis )
{ {
final Context context = getActivity(); final Context context = getActivity();
final DlgState state = getState( sis );
getBundleData( sis );
final DlgState state = getState();
NotAgainView naView = (NotAgainView) NotAgainView naView = (NotAgainView)
LocUtils.inflate( context, R.layout.not_again_view ); LocUtils.inflate( context, R.layout.not_again_view );

View file

@ -427,6 +427,8 @@ public class DelegateBase implements DlgClickNotify,
{ {
if ( m_activity instanceof MainActivity ) { if ( m_activity instanceof MainActivity ) {
((MainActivity)m_activity).show( df ); ((MainActivity)m_activity).show( df );
} else if ( m_activity instanceof PrefsActivity ) {
((PrefsActivity)m_activity).show( df );
} else { } else {
Assert.assertTrue( !BuildConfig.DEBUG ); Assert.assertTrue( !BuildConfig.DEBUG );
} }

View file

@ -43,10 +43,24 @@ public class DlgDelegateAlert extends DialogFragment {
private static final String STATE_KEY = "STATE_KEY"; private static final String STATE_KEY = "STATE_KEY";
private DlgState m_state; private DlgState m_state;
public DlgDelegateAlert( DlgState state ) { m_state = state; }
public DlgDelegateAlert() {} 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 @Override
public void onSaveInstanceState( Bundle bundle ) public void onSaveInstanceState( Bundle bundle )
@ -55,13 +69,6 @@ public class DlgDelegateAlert extends DialogFragment {
bundle.putParcelable( STATE_KEY, m_state ); 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 ) protected void checkNotAgainCheck( DlgState state, NotAgainView naView )
{ {
if ( null != naView && naView.getChecked() ) { if ( null != naView && naView.getChecked() ) {

View file

@ -20,6 +20,7 @@
package org.eehouse.android.xw4; package org.eehouse.android.xw4;
import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@ -27,6 +28,8 @@ import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.ActionPair; import org.eehouse.android.xw4.DlgDelegate.ActionPair;
public class DlgState implements Parcelable { public class DlgState implements Parcelable {
private static final String BUNDLE_KEY = "bk";
public DlgID m_id; public DlgID m_id;
public String m_msg; public String m_msg;
public int m_posButton; public int m_posButton;
@ -67,6 +70,18 @@ public class DlgState implements Parcelable {
return 0; 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 ) { public void writeToParcel( Parcel out, int flags ) {
out.writeInt( m_id.ordinal() ); out.writeInt( m_id.ordinal() );
out.writeInt( m_posButton ); out.writeInt( m_posButton );

View file

@ -45,12 +45,9 @@ public class InviteChoicesAlert extends DlgDelegateAlert {
public static InviteChoicesAlert newInstance( DlgState state ) public static InviteChoicesAlert newInstance( DlgState state )
{ {
return new InviteChoicesAlert( state ); InviteChoicesAlert result = new InviteChoicesAlert();
} result.addStateArgument( state );
return result;
public InviteChoicesAlert( DlgState state )
{
super( state );
} }
public InviteChoicesAlert() {} public InviteChoicesAlert() {}
@ -60,8 +57,7 @@ public class InviteChoicesAlert extends DlgDelegateAlert {
{ {
final Context context = getActivity(); final Context context = getActivity();
getBundleData( sis ); final DlgState state = getState( sis );
final DlgState state = getState();
final ArrayList<InviteMeans> means = final ArrayList<InviteMeans> means =
new ArrayList<InviteMeans>(); new ArrayList<InviteMeans>();

View file

@ -39,12 +39,9 @@ public class NotAgainAlert extends DlgDelegateAlert {
public static NotAgainAlert newInstance( DlgState state ) public static NotAgainAlert newInstance( DlgState state )
{ {
return new NotAgainAlert( state ); NotAgainAlert result = new NotAgainAlert();
} result.addStateArgument( state );
return result;
public NotAgainAlert( DlgState state )
{
super( state );
} }
public NotAgainAlert() {} public NotAgainAlert() {}
@ -54,8 +51,7 @@ public class NotAgainAlert extends DlgDelegateAlert {
{ {
final Context context = getActivity(); final Context context = getActivity();
getBundleData( sis ); final DlgState state = getState( sis );
final DlgState state = getState();
final NotAgainView naView = (NotAgainView) final NotAgainView naView = (NotAgainView)
LocUtils.inflate( context, R.layout.not_again_view ); LocUtils.inflate( context, R.layout.not_again_view );
@ -82,5 +78,4 @@ public class NotAgainAlert extends DlgDelegateAlert {
Dialog dialog = builder.create(); Dialog dialog = builder.create();
return dialog; return dialog;
} }
} }

View file

@ -39,12 +39,9 @@ public class OkOnlyAlert extends DlgDelegateAlert {
public static OkOnlyAlert newInstance( DlgState state ) public static OkOnlyAlert newInstance( DlgState state )
{ {
return new OkOnlyAlert( state ); OkOnlyAlert result = new OkOnlyAlert();
} result.addStateArgument( state );
return result;
public OkOnlyAlert( DlgState state )
{
super( state );
} }
public OkOnlyAlert() {} public OkOnlyAlert() {}
@ -54,8 +51,7 @@ public class OkOnlyAlert extends DlgDelegateAlert {
{ {
final Context context = getActivity(); final Context context = getActivity();
getBundleData( sis ); final DlgState state = getState( sis );
final DlgState state = getState();
Dialog dialog = LocUtils.makeAlertBuilder( getActivity() ) Dialog dialog = LocUtils.makeAlertBuilder( getActivity() )
.setTitle( state.m_titleId == 0 ? R.string.info_title : state.m_titleId ) .setTitle( state.m_titleId == 0 ? R.string.info_title : state.m_titleId )

View file

@ -24,6 +24,7 @@ import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.support.v4.app.DialogFragment;
import junit.framework.Assert; import junit.framework.Assert;
@ -153,4 +154,11 @@ public class PrefsActivity extends PreferenceActivity
public void addFragment( XWFragment fragment, Bundle extras ) { Assert.fail(); } public void addFragment( XWFragment fragment, Bundle extras ) { Assert.fail(); }
public void addFragmentForResult( XWFragment fragment, Bundle extras, public void addFragmentForResult( XWFragment fragment, Bundle extras,
RequestCode code ) { Assert.fail(); } 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();
}
} }