From d6b40709058cad1c46627f38688228db55b2f639 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 22 Jul 2016 12:46:01 -0700 Subject: [PATCH] hang onto DelegateBase instance until replaced via an onStart() call. They're often needed when the fragment isn't frontmost, i.e. when onPause() has been called. My caching instances fix is feeling a bit fragile, but I think it's better than nothing. Alternative is probably to go with DialogFragments, a big change that might not be easy to make work back to oldest Android. --- .../org/eehouse/android/xw4/DelegateBase.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 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 246cd0fa2..bf10f76a1 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java @@ -92,7 +92,6 @@ public class DelegateBase implements DlgClickNotify, public void onCreateContextMenu( ContextMenu menu, View view, ContextMenuInfo menuInfo ) {} public boolean onContextItemSelected( MenuItem item ) { return false; } - protected void onStart() {} protected void onStop() {} protected void onDestroy() {} protected void onWindowFocusChanged( boolean hasFocus ) {} @@ -121,10 +120,17 @@ public class DelegateBase implements DlgClickNotify, getClass().getSimpleName() ); } + protected void onStart() + { + if ( s_instances.containsKey(getClass()) ) { + DbgUtils.logdf( "%s.onStart(): replacing curThis", + getClass().getSimpleName() ); + } + s_instances.put( getClass(), new WeakReference(this) ); + } + protected void onResume() { - Assert.assertFalse( s_instances.containsKey(getClass()) ); - s_instances.put( getClass(), new WeakReference(this) ); m_isVisible = true; XWService.setListener( this ); runIfVisible(); @@ -132,16 +138,18 @@ public class DelegateBase implements DlgClickNotify, protected void onPause() { - s_instances.remove( getClass() ); m_isVisible = false; XWService.setListener( null ); } protected DelegateBase curThis() { + DelegateBase result = null; WeakReference ref = s_instances.get( getClass() ); - DelegateBase result = ref.get(); - DbgUtils.logf( "%s.curThis() => %s", this.toString(), result.toString() ); + if ( null != ref ) { + result = ref.get(); + } + // DbgUtils.logdf( "%s.curThis() => " + result, this.toString() ); return result; }