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.
This commit is contained in:
Eric House 2016-07-21 07:03:44 -07:00
parent c57b5a232f
commit a8b2c09491
5 changed files with 63 additions and 48 deletions

View file

@ -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 );
}

View file

@ -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 );

View file

@ -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:

View file

@ -221,44 +221,48 @@ public class MainActivity extends XWActivity
addFragmentImpl( fragment, extras, fragment.getParent() );
}
@Override
private class PendingResultCache {
private WeakReference<Fragment> 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<Fragment>(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<DelegateBase> ref
= new WeakReference<DelegateBase>(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<RequestCode> iter = m_pendingCodes.keySet().iterator();
while ( iter.hasNext() ) {
RequestCode key = iter.next();
WeakReference<DelegateBase> 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;
}
}
}

View file

@ -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; }