@@ -573,6 +573,9 @@ public synchronized boolean handleEvent(ActivationStateMachine.Event event) {
573573 // An event's side effects may end up synchronously calling handleEvent while it's
574574 // itself being handled. In that case, enqueue it so it's handled next (and still
575575 // synchronously).
576+ //
577+ // We don't need to persist here, as the handleEvent call up the stack will eventually
578+ // persist when done with the synchronous transitions.
576579 enqueueEvent (event );
577580 return true ;
578581 }
@@ -584,7 +587,7 @@ public synchronized boolean handleEvent(ActivationStateMachine.Event event) {
584587 ActivationStateMachine .State maybeNext = current .transition (event );
585588 if (maybeNext == null ) {
586589 enqueueEvent (event );
587- return true ;
590+ return persist () ;
588591 }
589592
590593 Log .d (TAG , String .format ("transition: %s -(%s)-> %s" , current .getClass ().getSimpleName (), event .getClass ().getSimpleName (), maybeNext .getClass ().getSimpleName ()));
@@ -675,7 +678,18 @@ private ArrayDeque<ActivationStateMachine.Event> getPersistedPendingEvents() {
675678 for (int i = 0 ; i < length ; i ++) {
676679 try {
677680 String className = activationContext .getPreferences ().getString (String .format ("%s[%d]" , ActivationStateMachine .PersistKeys .PENDING_EVENTS_PREFIX , i ), "" );
678- ActivationStateMachine .Event event = ((Class <ActivationStateMachine .Event >) Class .forName (className )).newInstance ();
681+ Class <ActivationStateMachine .Event > eventClass = (Class <ActivationStateMachine .Event >) Class .forName (className );
682+ ActivationStateMachine .Event event ;
683+ try {
684+ event = eventClass .newInstance ();
685+ } catch (InstantiationException e ) {
686+ // We aren't properly persisting events with a non-nullary constructor. Those events
687+ // are supposed to be handled by states that aren't persisted (until
688+ // https://github.com/ably/ably-java/issues/546 is fixed), so it should be safe to
689+ // just drop them.
690+ Log .d (TAG , String .format ("discarding improperly persisted event: %s" , className ));
691+ continue ;
692+ }
679693 deque .add (event );
680694 } catch (Exception e ) {
681695 throw new RuntimeException (e );
0 commit comments