try to dismiss duplicate alerts

Each alert gets a unique name. Add to backstack, and before doing so
look for another with the same name and dismiss it. This works most of
the time, especially to prevent them from piling up with orientation
changes. But in a robot-vs-robot game, and occasionally in a game with
three robots and one not, SCORE alerts stack up. It's the removal that's
failing, not the test for a duplicate, so perhaps I need to somehow wait
for it to succeed before adding the new alert.
This commit is contained in:
Eric House 2017-04-12 07:37:53 -07:00
parent fdd1e5c148
commit 5bbfd44333
11 changed files with 93 additions and 32 deletions

View file

@ -37,6 +37,7 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class AboutAlert extends XWDialogFragment {
private static final String TAG = AboutAlert.class.getSimpleName();
public static AboutAlert newInstance()
{
@ -82,4 +83,7 @@ public class AboutAlert extends XWDialogFragment {
.setPositiveButton( android.R.string.ok, null )
.create();
}
@Override
protected String getFragTag() { return TAG; }
}

View file

@ -309,9 +309,8 @@ public class BoardDelegate extends DelegateBase
int title = (Integer)params[0];
String msg = (String)params[1];
ab.setMessage( msg );
if ( 0 != title ) {
ab.setTitle( title );
}
Assert.assertTrue( 0 != title );
ab.setTitle( title );
ab.setPositiveButton( android.R.string.ok, null );
if ( DlgID.DLG_SCORES == dlgID ) {
if ( null != m_mySIS.words && m_mySIS.words.length > 0 ) {

View file

@ -26,6 +26,9 @@ import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.eehouse.android.xw4.loc.LocUtils;
@ -62,12 +65,23 @@ public class DBAlert extends XWDialogFragment {
public DBAlert() {}
public DlgID getDlgID() { return mDlgID; }
public DlgID getDlgID() {
if ( null == mDlgID ) {
mDlgID = DlgID.values()[getArguments().getInt(DLG_ID_KEY, -1)];
}
return mDlgID;
}
@Override
public String getFragTag()
{
return getDlgID().toString();
}
@Override
public void onSaveInstanceState( Bundle bundle )
{
bundle.putInt( DLG_ID_KEY, mDlgID.ordinal() );
bundle.putInt( DLG_ID_KEY, getDlgID().ordinal() );
bundle.putSerializable( PARMS_KEY, mParams );
super.onSaveInstanceState( bundle );
}
@ -78,7 +92,6 @@ public class DBAlert extends XWDialogFragment {
if ( null == sis ) {
sis = getArguments();
}
mDlgID = DlgID.values()[sis.getInt(DLG_ID_KEY, -1)];
mParams = (Object[])sis.getSerializable(PARMS_KEY);
XWActivity activity = (XWActivity)getActivity();
@ -87,7 +100,8 @@ public class DBAlert extends XWDialogFragment {
if ( null == dialog ) {
dialog = LocUtils.makeAlertBuilder( getActivity() )
.setTitle( "Stub Alert" )
.setMessage( String.format( "Unable to create for %s", mDlgID.toString() ) )
.setMessage( String.format( "Unable to create for %s",
getDlgID().toString() ) )
.setPositiveButton( "Bummer", null )
// .setNegativeButton( "Try now", new OnClickListener() {
// @Override
@ -103,7 +117,7 @@ public class DBAlert extends XWDialogFragment {
public void run() {
MainActivity activity = (MainActivity)getActivity();
if ( null != activity ) {
DBAlert newMe = newInstance( mDlgID, mParams );
DBAlert newMe = newInstance( getDlgID(), mParams );
activity.show( newMe );
dismiss(); // kill myself...
} else {

View file

@ -438,8 +438,7 @@ public class DelegateBase implements DlgClickNotify,
protected void showDialogFragment( DlgID dlgID, Object... params )
{
DialogFragment fragment = DBAlert.newInstance( dlgID, params );
show( fragment );
show( DBAlert.newInstance( dlgID, params ) );
}
protected void show( DlgState state )
@ -468,7 +467,7 @@ public class DelegateBase implements DlgClickNotify,
show( df );
}
protected void show( DialogFragment df )
protected void show( XWDialogFragment df )
{
if ( m_activity instanceof XWActivity ) {
((XWActivity)m_activity).show( df );

View file

@ -47,12 +47,14 @@ abstract class DlgDelegateAlert extends XWDialogFragment {
protected final DlgState getState( Bundle sis )
{
if ( null != sis ) {
m_state = (DlgState)sis.getParcelable( STATE_KEY );
} else {
Bundle args = getArguments();
Assert.assertNotNull( args );
m_state = DlgState.fromBundle( args );
if ( m_state == null ) {
if ( null != sis ) {
m_state = (DlgState)sis.getParcelable( STATE_KEY );
} else {
Bundle args = getArguments();
Assert.assertNotNull( args );
m_state = DlgState.fromBundle( args );
}
}
return m_state;
}
@ -80,6 +82,12 @@ abstract class DlgDelegateAlert extends XWDialogFragment {
super.onDismiss( dif );
}
@Override
protected String getFragTag()
{
return getState(null).m_id.toString();
}
protected void checkNotAgainCheck( DlgState state, NotAgainView naView )
{
if ( null != naView && naView.getChecked() ) {

View file

@ -22,7 +22,6 @@ package org.eehouse.android.xw4;
public enum DlgID {
NONE
, LOOKUP
, CHANGE_GROUP
, CONFIRM_CHANGE
, CONFIRM_CHANGE_PLAY
@ -64,7 +63,6 @@ public enum DlgID {
, ASK_PASSWORD
, DLG_RETRY
, DLG_SCORES
, PICK_TILE_REQUESTTRAY
, DLG_USEDICT
, DLG_GETDICT
, GAMES_LIST_NEWGAME

View file

@ -31,6 +31,7 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class LookupAlert extends XWDialogFragment {
private static final String TAG = LookupAlert.class.getSimpleName();
private LookupAlertView m_view;
public static LookupAlert newInstance( String[] words, int lang, boolean noStudy )
@ -73,4 +74,7 @@ public class LookupAlert extends XWDialogFragment {
result.setOnKeyListener( m_view );
return result;
}
@Override
protected String getFragTag() { return TAG; }
}

View file

@ -357,16 +357,29 @@ public class MainActivity extends XWActivity
private void logPaneFragments()
{
if ( BuildConfig.DEBUG && null != m_root ) {
List<String> pairs = new ArrayList<>();
int childCount = m_root.getChildCount();
for ( int ii = 0; ii < childCount; ++ii ) {
View child = m_root.getChildAt( ii );
String name = findFragment( child ).getClass().getSimpleName();
String pair = String.format("%d:%s", ii, name );
pairs.add( pair );
if ( BuildConfig.DEBUG ) {
List<String> panePairs = new ArrayList<>();
if ( null != m_root ) {
int childCount = m_root.getChildCount();
for ( int ii = 0; ii < childCount; ++ii ) {
View child = m_root.getChildAt( ii );
String name = findFragment( child ).getClass().getSimpleName();
String pair = String.format("%d:%s", ii, name );
panePairs.add( pair );
}
}
Log.d( TAG, "logPaneFragments(): %s", TextUtils.join(", ", pairs) );
FragmentManager fm = getSupportFragmentManager();
List<String> fragPairs = new ArrayList<>();
int fragCount = fm.getBackStackEntryCount();
for ( int ii = 0; ii < fragCount; ++ii ) {
FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt( ii );
String name = entry.getName();
String pair = String.format("%d:%s", ii, name );
fragPairs.add( pair );
}
Log.d( TAG, "panes: [%s]; frags: [%s]", TextUtils.join(",", panePairs),
TextUtils.join(",", fragPairs) );
}
}

View file

@ -1,4 +1,4 @@
/* -*- compile-command: "cd ../../../../../../../../ && ./gradlew installXw4Debug"; -*- */
/* -*- compile-command: "find-and-gradle.sh insXw4Deb"; -*- */
/*
* Copyright 2017 by Eric House (xwords@eehouse.org). All rights reserved.
*
@ -39,6 +39,7 @@ import org.eehouse.android.xw4.loc.LocUtils;
public class TilePickAlert extends XWDialogFragment
implements TilePickView.TilePickListener {
private static final String TAG = TilePickAlert.class.getSimpleName();
private static final String TPS = "TPS";
private static final String ACTION = "ACTION";
private TilePickView m_view;
@ -127,6 +128,8 @@ public class TilePickAlert extends XWDialogFragment
return m_dialog;
}
protected String getFragTag() { return TAG; }
// TilePickView.TilePickListener interface
@Override
public void onTilesChanged( int nToPick, int[] newTiles )

View file

@ -26,7 +26,10 @@ import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ContextMenu;
import android.view.Menu;
@ -278,9 +281,23 @@ public class XWActivity extends FragmentActivity
Assert.fail();
}
protected void show( DialogFragment df )
protected void show( XWDialogFragment df )
{
df.show( getSupportFragmentManager(), "dialog" );
FragmentManager fm = getSupportFragmentManager();
String tag = df.getFragTag();
Log.d( TAG, "%s.show(%s) called; tag: %s", getClass().getSimpleName(),
df.getClass().getSimpleName(), tag );
FragmentTransaction trans = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag( tag );
if ( null != prev && prev instanceof DialogFragment ) {
Log.d( TAG, "removing %s (tag %s)",
prev.getClass().getSimpleName(), tag );
((DialogFragment)prev).dismiss();
}
trans.addToBackStack( tag );
// Create and show the dialog. show() commits the transaction
df.show( trans, tag );
}
protected Dialog makeDialog( DBAlert alert, Object[] params )

View file

@ -32,7 +32,7 @@ import java.util.Map;
import junit.framework.Assert;
public class XWDialogFragment extends DialogFragment {
abstract class XWDialogFragment extends DialogFragment {
private static final String TAG = XWDialogFragment.class.getSimpleName();
private OnDismissListener m_onDismiss;
@ -87,6 +87,8 @@ public class XWDialogFragment extends DialogFragment {
super.onDismiss( dif );
}
abstract String getFragTag();
protected void setOnDismissListener( OnDismissListener lstnr )
{
Assert.assertTrue( null == lstnr || null == m_onDismiss || !BuildConfig.DEBUG );