From 80e1f588d07a5ce8cd6e7a84a3c3a74f3b540e68 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 12 Aug 2016 18:06:11 -0700 Subject: [PATCH] remove a delegate's alerts when it stops Fixing a crash when a game offers both to create a rematch and to delete itself because its opponent has been deleted. Deleting and then hitting rematch would crash because the rematch alert referred to a dead game. Now onStop() for delegates removes any pending alerts. In dual-pane mode only since when there's an activity going away the alerts go automatically. --- .../org/eehouse/android/xw4/DelegateBase.java | 10 +++++++++- .../org/eehouse/android/xw4/DictsDelegate.java | 2 ++ .../org/eehouse/android/xw4/DlgDelegate.java | 18 ++++++++++++++++++ .../eehouse/android/xw4/GamesListDelegate.java | 3 ++- 4 files changed, 31 insertions(+), 2 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 7e00a7bbc..dda5645e3 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, protected void onCreateContextMenu( ContextMenu menu, View view, ContextMenuInfo menuInfo ) {} protected boolean onContextItemSelected( MenuItem item ) { return false; } - protected void onStop() {} protected void onDestroy() {} protected void onWindowFocusChanged( boolean hasFocus ) {} protected boolean handleBackPressed() { return false; } @@ -142,6 +141,15 @@ public class DelegateBase implements DlgClickNotify, XWService.setListener( null ); } + protected void onStop() + { + // Alerts disappear on their own if not in dualpane mode + if ( m_activity instanceof MainActivity + && ((MainActivity)m_activity).inDPMode() ) { + DlgDelegate.closeAlerts( m_activity, this ); + } + } + protected DelegateBase curThis() { DelegateBase result = null; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java index 05c56341a..579dff15e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -552,9 +552,11 @@ public class DictsDelegate extends ListDelegateBase setTitleBar(); } + @Override protected void onStop() { MountEventReceiver.unregister( this ); + super.onStop(); } public void onClick( View view ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java index 6ad2f3275..48e264110 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java @@ -938,6 +938,24 @@ public class DlgDelegate { } } + protected static void closeAlerts( Activity activity, DelegateBase base ) + { + DbgUtils.assertOnUIThread(); + Iterator iter = s_pendings.keySet().iterator(); + while ( iter.hasNext() ) { + DlgID dlgID = iter.next(); + DelegateBase oneBase = s_pendings.get( dlgID ).get(); + if ( null == oneBase ) { + iter.remove(); // no point in keeping it + } else if ( base.equals( oneBase ) ) { + DbgUtils.logdf( "removing alert %s for %s", dlgID.toString(), + oneBase.toString() ); + activity.removeDialog( dlgID.ordinal() ); + iter.remove(); // no point in keeping this either + } + } + } + private String getString( int id, Object... params ) { return m_dlgt.getString( id, params ); 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 964e9f740..e2c8e254c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java @@ -1004,6 +1004,7 @@ public class GamesListDelegate extends ListDelegateBase return true; // handled it } + @Override protected void onStop() { // TelephonyManager mgr = @@ -1012,7 +1013,7 @@ public class GamesListDelegate extends ListDelegateBase // m_phoneStateListener = null; long[] positions = m_adapter.getGroupPositions(); XWPrefs.setGroupPositions( m_activity, positions ); - // super.onStop(); + super.onStop(); } protected void onDestroy()