From a8b2c094916992db1300a8d3bba3422665b44b91 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 21 Jul 2016 07:03:44 -0700 Subject: [PATCH] fix startFragmentForResult use s/getTargetFragment(), but there's still the hack of capturing state in setResult() and then invoking fragment.onActivityResult() blindly the next time the backstack is popped. --- .../org/eehouse/android/xw4/DelegateBase.java | 3 +- .../org/eehouse/android/xw4/GameUtils.java | 16 ---- .../android/xw4/GamesListDelegate.java | 4 +- .../org/eehouse/android/xw4/MainActivity.java | 76 ++++++++++++------- .../org/eehouse/android/xw4/XWFragment.java | 12 ++- 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java index 3fbd3a0b6..857f5488e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java @@ -216,7 +216,8 @@ public class DelegateBase implements DlgClickNotify, { if ( m_activity instanceof MainActivity ) { MainActivity main = (MainActivity)m_activity; - main.setFragmentResult( this, result, intent ); + XWFragment fragment = (XWFragment)m_delegator; + main.setFragmentResult( fragment, result, intent ); } else { m_activity.setResult( result, intent ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index a83b3a118..3cc5bd13b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -1087,22 +1087,6 @@ public class GameUtils { DBUtils.saveSummary( context, lock, summary ); } // applyChanges - public static void doConfig( Delegator delegator, long rowid ) - { - Bundle extras = new Bundle(); - extras.putLong( INTENT_KEY_ROWID, rowid ); - - if ( delegator.inDPMode() ) { - delegator.addFragment( new GameConfigFrag( delegator ), extras ); - } else { - Activity activity = delegator.getActivity(); - Intent intent = new Intent( activity, GameConfigActivity.class ); - intent.setAction( Intent.ACTION_EDIT ); - intent.putExtras( extras ); - activity.startActivity( intent ); - } - } - public static String formatGameID( int gameID ) { Assert.assertTrue( 0 != gameID ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java index da3cc9843..e8ca74ee1 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java @@ -1678,7 +1678,9 @@ public class GamesListDelegate extends ListDelegateBase break; case R.id.games_game_config: - GameUtils.doConfig( getDelegator(), selRowIDs[0] ); + GameConfigDelegate.editForResult( getDelegator(), + RequestCode.CONFIG_GAME, + selRowIDs[0] ); break; case R.id.games_game_move: diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MainActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MainActivity.java index 74ef5b6aa..1dad7d8e9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MainActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MainActivity.java @@ -221,44 +221,48 @@ public class MainActivity extends XWActivity addFragmentImpl( fragment, extras, fragment.getParent() ); } - @Override + private class PendingResultCache { + private WeakReference m_frag; + public int m_request; + public int m_result; + public Intent m_data; + public PendingResultCache( Fragment target, int request, int result, Intent data ) { + m_frag = new WeakReference(target); + m_request = request; + m_result = result; + m_data = data; + } + + public Fragment getTarget() { return m_frag.get(); } + } + private PendingResultCache m_pendingResult; + public void addFragmentForResult( XWFragment fragment, Bundle extras, - RequestCode requestCode ) + RequestCode requestCode, XWFragment registrant ) { DbgUtils.assertOnUIThread(); - WeakReference ref - = new WeakReference(fragment.getDelegate()); - m_pendingCodes.put( requestCode, ref ); + fragment.setTargetFragment( registrant, requestCode.ordinal() ); addFragmentImpl( fragment, extras, fragment.getParent() ); } - protected void setFragmentResult( DelegateBase delegate, int resultCode, + protected void setFragmentResult( XWFragment fragment, int resultCode, Intent data ) { - DbgUtils.assertOnUIThread(); - RequestCode requestCode = null; - Iterator iter = m_pendingCodes.keySet().iterator(); - while ( iter.hasNext() ) { - RequestCode key = iter.next(); - WeakReference ref = m_pendingCodes.get(key); - DelegateBase thisOne = ref.get(); - if ( null != thisOne && thisOne.equals(delegate) ) { - requestCode = key; - iter.remove(); - break; - } - } + Fragment target = fragment.getTargetFragment(); + int requestCode = fragment.getTargetRequestCode(); - if ( null != requestCode ) { - delegate.onActivityResult( requestCode, resultCode, data ); - } + Assert.assertNull( m_pendingResult ); + m_pendingResult = new PendingResultCache( target, requestCode, + resultCode, data ); } protected void finishFragment() { - getSupportFragmentManager().popBackStack(); + // Assert.assertTrue( fragment instanceof XWFragment ); + // DbgUtils.logf( "MainActivity.finishFragment(%s)", fragment.toString() ); + getSupportFragmentManager().popBackStack/*Immediate*/(); } ////////////////////////////////////////////////////////////////////// @@ -271,12 +275,26 @@ public class MainActivity extends XWActivity int fragCount = getSupportFragmentManager().getBackStackEntryCount(); if ( 0 == fragCount ) { finish(); - } else if ( fragCount == m_root.getChildCount() - 1 ) { - View child = m_root.getChildAt( fragCount ); - // DbgUtils.logf( "onBackStackChanged(): removing view with id %x", - // child.getId() ); - m_root.removeView( child ); - setVisiblePanes(); + } else { + if ( fragCount == m_root.getChildCount() - 1 ) { + View child = m_root.getChildAt( fragCount ); + // DbgUtils.logf( "onBackStackChanged(): removing view with id %x", + // child.getId() ); + m_root.removeView( child ); + setVisiblePanes(); + } + + // If there's a pending on-result call, make it. + if ( null != m_pendingResult ) { + Fragment target = m_pendingResult.getTarget(); + if ( null != target ) { + DbgUtils.logf( "onBackStackChanged(): calling onActivityResult()" ); + target.onActivityResult( m_pendingResult.m_request, + m_pendingResult.m_result, + m_pendingResult.m_data ); + } + m_pendingResult = null; + } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWFragment.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWFragment.java index ac223f47c..87bcbf9ba 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWFragment.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWFragment.java @@ -20,6 +20,7 @@ package org.eehouse.android.xw4; +import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -120,6 +121,15 @@ public class XWFragment extends Fragment implements Delegator { super.onDestroy(); } + @Override + public void onActivityResult( int requestCode, int resultCode, Intent data ) + { + DbgUtils.logdf( "%s.onActivityResult() called", + this.getClass().getSimpleName() ); + m_dlgt.onActivityResult( RequestCode.values()[requestCode], + resultCode, data ); + } + @Override public void onPrepareOptionsMenu( Menu menu ) { @@ -162,7 +172,7 @@ public class XWFragment extends Fragment implements Delegator { RequestCode code ) { MainActivity main = (MainActivity)getActivity(); - main.addFragmentForResult( fragment, extras, code ); + main.addFragmentForResult( fragment, extras, code, this ); } public DelegateBase getDelegate() { return m_dlgt; }