add no-args constructors to all fragments

Docs and some logcat crash statements from the OS say I need no-args
constructors for fragments. So rework initialization and use bundling so
parent name doesn't have to be passed into the constructor. Seems to
work, and fixes the crash I was seeing (happened when sending an
invitation via SMS) but requires more testing.
This commit is contained in:
Eric House 2016-08-13 17:02:18 -07:00
parent 94e5b30d94
commit 6bdfae23cb
15 changed files with 98 additions and 28 deletions

View file

@ -24,7 +24,12 @@ import android.os.Bundle;
public class BoardFrag extends XWFragment {
public BoardFrag( Delegator parent ) { super( parent ); }
public BoardFrag() {}
public static XWFragment newInstance( Delegator parent )
{
return new BoardFrag().setParentName( parent );
}
@Override
public void onCreate( Bundle sis )

View file

@ -264,7 +264,7 @@ public class ChatDelegate extends DelegateBase {
bundle.putBooleanArray( INTENT_KEY_LOCS, locs );
if ( delegator.inDPMode() ) {
delegator.addFragment( new ChatFrag( delegator ), bundle );
delegator.addFragment( ChatFrag.newInstance( delegator ), bundle );
} else {
Activity activity = delegator.getActivity();
Intent intent = new Intent( activity, ChatActivity.class );

View file

@ -23,7 +23,12 @@ import android.os.Bundle;
public class ChatFrag extends XWFragment {
public ChatFrag( Delegator parent ) { super( parent ); }
public ChatFrag() {}
public static XWFragment newInstance( Delegator parent )
{
return new ChatFrag().setParentName( parent );
}
@Override
public void onCreate( Bundle sis )

View file

@ -421,7 +421,8 @@ public class DictBrowseDelegate extends ListDelegateBase
private static void launch( Delegator delegator, Bundle bundle )
{
if ( delegator.inDPMode() ) {
delegator.addFragment( new DictBrowseFrag( delegator ), bundle );
delegator.addFragment( DictBrowseFrag.newInstance( delegator ),
bundle );
} else {
Activity activity = delegator.getActivity();
Intent intent = new Intent( activity, DictBrowseActivity.class );

View file

@ -23,7 +23,12 @@ import android.os.Bundle;
public class DictBrowseFrag extends XWFragment {
public DictBrowseFrag( Delegator parent ) { super( parent ); }
public DictBrowseFrag() {}
public static XWFragment newInstance( Delegator parent )
{
return new DictBrowseFrag().setParentName( parent );
}
@Override
public void onCreate( Bundle sis )

View file

@ -1455,7 +1455,7 @@ public class DictsDelegate extends ListDelegateBase
public static void start( Delegator delegator )
{
if ( delegator.inDPMode() ) {
delegator.addFragment( new DictsFrag( delegator ), null );
delegator.addFragment( DictsFrag.newInstance( delegator ), null );
} else {
Activity activity = delegator.getActivity();
Intent intent = new Intent( activity, DictsActivity.class );

View file

@ -23,7 +23,12 @@ import android.os.Bundle;
public class DictsFrag extends XWFragment {
public DictsFrag( Delegator parent ) { super( parent ); }
public DictsFrag() {}
public static XWFragment newInstance( Delegator parent )
{
return new DictsFrag().setParentName( parent );
}
@Override
public void onCreate( Bundle sis )

View file

@ -1272,8 +1272,9 @@ public class GameConfigDelegate extends DelegateBase
bundle.putBoolean( INTENT_FORRESULT_NEWGAME, newGame );
if ( delegator.inDPMode() ) {
delegator.addFragmentForResult( new GameConfigFrag( delegator ),
bundle, requestCode );
delegator
.addFragmentForResult( GameConfigFrag.newInstance( delegator ),
bundle, requestCode );
} else {
Activity activity = delegator.getActivity();
Intent intent = new Intent( activity, GameConfigActivity.class );

View file

@ -23,7 +23,12 @@ import android.os.Bundle;
public class GameConfigFrag extends XWFragment {
public GameConfigFrag( Delegator parent ) { super( parent ); }
public GameConfigFrag() {}
public static XWFragment newInstance( Delegator parent )
{
return new GameConfigFrag().setParentName( parent );
}
@Override
public void onCreate( Bundle sis )

View file

@ -904,7 +904,7 @@ public class GameUtils {
{
Bundle extras = makeLaunchExtras( rowid, invited );
if ( delegator.inDPMode() ) {
delegator.addFragment( new BoardFrag( delegator ), extras );
delegator.addFragment( BoardFrag.newInstance( delegator ), extras );
} else {
Activity activity = delegator.getActivity();
Intent intent = new Intent( activity, BoardActivity.class );

View file

@ -24,7 +24,12 @@ import android.os.Bundle;
public class GamesListFrag extends XWFragment {
public GamesListFrag( Delegator parent ) { super( parent ); }
public GamesListFrag() {}
public static XWFragment newInstance()
{
return new GamesListFrag().setParentName(null);
}
@Override
public void onCreate( Bundle sis )

View file

@ -80,7 +80,7 @@ public class MainActivity extends XWActivity
if ( savedInstanceState == null ) {
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
addFragmentImpl( new GamesListFrag(null),
addFragmentImpl( GamesListFrag.newInstance(),
getIntent().getExtras(), null );
}
}
@ -244,7 +244,7 @@ public class MainActivity extends XWActivity
@Override
public void addFragment( XWFragment fragment, Bundle extras )
{
addFragmentImpl( fragment, extras, fragment.getParent() );
addFragmentImpl( fragment, extras, fragment.getParentName() );
}
private class PendingResultCache {
@ -270,7 +270,7 @@ public class MainActivity extends XWActivity
fragment.setTargetFragment( registrant, requestCode.ordinal() );
addFragmentImpl( fragment, extras, fragment.getParent() );
addFragmentImpl( fragment, extras, fragment.getParentName() );
}
protected void setFragmentResult( XWFragment fragment, int resultCode,
@ -423,13 +423,13 @@ public class MainActivity extends XWActivity
}
private void addFragmentImpl( Fragment fragment, Bundle bundle,
Delegator parent )
String parentName )
{
fragment.setArguments( bundle );
addFragmentImpl( fragment, parent );
addFragmentImpl( fragment, parentName );
}
private void addFragmentImpl( Fragment fragment, Delegator parent )
private void addFragmentImpl( Fragment fragment, String parentName )
{
String newName = fragment.getClass().getSimpleName();
boolean replace = false;
@ -447,9 +447,8 @@ public class MainActivity extends XWActivity
FragmentManager.BackStackEntry entry
= fm.getBackStackEntryAt( fragCount - 2 );
String curName = entry.getName();
String delName = parent.getClass().getSimpleName();
// DbgUtils.logf( "comparing %s, %s", curName, delName );
replace = curName.equals( delName );
// DbgUtils.logf( "comparing %s, %s", curName, parentName );
replace = curName.equals( parentName );
}
if ( replace ) {

View file

@ -361,7 +361,8 @@ public class StudyListDelegate extends ListDelegateBase
}
if ( delegator.inDPMode() ) {
delegator.addFragment( new StudyListFrag( delegator ), bundle );
delegator.addFragment( StudyListFrag.newInstance( delegator ),
bundle );
} else {
Intent intent = new Intent( activity, StudyListActivity.class );
intent.putExtras( bundle );

View file

@ -24,7 +24,12 @@ import android.os.Bundle;
public class StudyListFrag extends XWFragment {
public StudyListFrag( Delegator parent ) { super( parent ); }
public StudyListFrag() {}
public static XWFragment newInstance( Delegator parent )
{
return new StudyListFrag().setParentName( parent );
}
@Override
public void onCreate( Bundle sis )

View file

@ -20,6 +20,7 @@
package org.eehouse.android.xw4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@ -34,15 +35,25 @@ import android.widget.ListView;
import junit.framework.Assert;
public class XWFragment extends Fragment implements Delegator {
abstract class XWFragment extends Fragment implements Delegator {
private static final String PARENT_NAME = "PARENT_NAME";
private DelegateBase m_dlgt;
private Delegator m_parent;
private String m_parentName;
private boolean m_hasOptionsMenu = false;
public XWFragment( Delegator parent ) { m_parent = parent; }
public XWFragment setParentName( Delegator parent )
{
m_parentName = null == parent ? "<none>"
: parent.getClass().getSimpleName();
return this;
}
public Delegator getParent() { return m_parent; }
public String getParentName()
{
Assert.assertNotNull( m_parentName );
return m_parentName;
}
protected void onCreate( DelegateBase dlgt, Bundle sis, boolean hasOptionsMenu )
{
@ -50,13 +61,35 @@ public class XWFragment extends Fragment implements Delegator {
this.onCreate( dlgt, sis );
}
@Override
public void onSaveInstanceState( Bundle outState )
{
super.onSaveInstanceState( outState );
Assert.assertNotNull( m_parentName );
outState.putString( PARENT_NAME, m_parentName );
}
protected void onCreate( DelegateBase dlgt, Bundle sis )
{
DbgUtils.logdf( "%s.onCreate() called", this.getClass().getSimpleName() );
super.onCreate( sis );
if ( null != sis ) {
m_parentName = sis.getString( PARENT_NAME );
Assert.assertNotNull( m_parentName );
}
m_dlgt = dlgt;
}
// This is supposed to be the first call we can use to start hooking stuff
// up.
// @Override
// public void onAttach( Activity activity )
// {
// DbgUtils.logdf( "%s.onAttach() called",
// this.getClass().getSimpleName() );
// super.onAttach( activity );
// }
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
@ -159,7 +192,7 @@ public class XWFragment extends Fragment implements Delegator {
@Override
public boolean inDPMode() {
MainActivity main = (MainActivity)getActivity();
Assert.assertTrue( main.inDPMode() ); // otherwise should be somewhere else
Assert.assertTrue( !isAdded() || main.inDPMode() ); // otherwise should be somewhere else
return true;
}