fix onResult stuff, at least for game config

This commit is contained in:
Eric House 2016-07-18 12:54:52 -07:00
parent 5bb707fc57
commit 1ed9dc7c9d
3 changed files with 40 additions and 3 deletions

View file

@ -212,7 +212,8 @@ public class DelegateBase implements DlgClickNotify,
protected void setResult( int result, Intent intent )
{
if ( m_activity instanceof MainActivity ) {
Assert.fail();
MainActivity main = (MainActivity)m_activity;
main.setFragmentResult( this, result, intent );
} else {
m_activity.setResult( result, intent );
}

View file

@ -1213,7 +1213,7 @@ public class GameConfigDelegate extends DelegateBase
bundle.putLong( GameUtils.INTENT_KEY_ROWID, rowID );
bundle.putBoolean( INTENT_FORRESULT_ROWID, true );
if ( false && delegator.inDPMode() ) {
if ( delegator.inDPMode() ) {
delegator.addFragmentForResult( new GameConfigFrag(), bundle, requestCode );
} else {
Activity activity = delegator.getActivity();

View file

@ -39,6 +39,11 @@ import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.LinearLayout;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eehouse.android.xw4.jni.CurGameInfo;
import junit.framework.Assert;
@ -56,6 +61,10 @@ public class MainActivity extends XWActivity
private int m_nextID = 0x00FFFFFF;
private Boolean m_isPortrait;
// for tracking launchForResult callback recipients
private Map<RequestCode, WeakReference<DelegateBase>> m_pendingCodes
= new HashMap<RequestCode, WeakReference<DelegateBase>>();
@Override
protected void onCreate( Bundle savedInstanceState )
{
@ -215,10 +224,37 @@ public class MainActivity extends XWActivity
public void addFragmentForResult( XWFragment fragment, Bundle extras,
RequestCode requestCode )
{
DbgUtils.logf( "addFragmentForResult(): dropping requestCode" );
DbgUtils.assertOnUIThread();
WeakReference<DelegateBase> ref
= new WeakReference<DelegateBase>(fragment.getDelegate());
m_pendingCodes.put( requestCode, ref );
addFragmentImpl( fragment, extras, this );
}
protected void setFragmentResult( DelegateBase delegate, 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;
}
}
if ( null != requestCode ) {
delegate.onActivityResult( requestCode, resultCode, data );
}
}
protected void finishFragment()
{
getSupportFragmentManager().popBackStack();