lukaspili / Mortar-architect

[DISCONTINUED] Navigation stack for Mortar. Alternative to Flow. Focuses on Mortar scopes, simplicity, seamless integration and killing boilerplate code.
MIT License
164 stars 12 forks source link

Crash when launching the main Activity with a PendingIntent when it is already running #10

Closed 0xjohnnycagewins closed 8 years ago

0xjohnnycagewins commented 9 years ago

When starting the main Activity with a PendingIntent (for example when taping on a push notification), this latter goes through the normal lifecycle (onCreate -> onStart ...). If the app is running when the intent is received, the Navigator object still exists in the Activity and therefore can't be set using mNavigator = ActivityArchitector.onCreateNavigator(...) in onCreate method because this method calls navigator.delegate().onCreate(...) which sends an exception if the container view is not null.

As a temp fix, we modified the code in the Activity's onCreate method to:

mNavigator = Navigator.find(this);
if (mNavigator == null) {
    mNavigator = ActivityArchitector.onCreateNavigator(...);
} else {
    mNavigator.backToRoot();
}

However, in onStart method mNavigator.delegate().onStart() is called which throws an exception is the dispatching callback is not null, which is the case in that situation. Not sure what would be the best fix for that, because I don't see why this check is necessary. If it is, there should be a way to manually deactivate the dispatcher.

Also, interesting stuff, we tried to temporary hack it using this code:

mNavigator = Navigator.find(this);
if (mNavigator != null) {
    mNavigator.delegate().onStop();
    mNavigator.delegate().onDestroy();
    // simulate history clearing
    mNavigator.backToRoot();
    mNavigator.back();
}
mNavigator = ActivityArchitector.onCreateNavigator(...);

For some reason we can't explain (we would have to dig deeper into the code), it does not work. It seems like the Mortar scope gets cleared. Any ideas? Also, I guess a method to clear the history (that would call this.history.killAll(true)) would be useful in Navigator.

0xjohnnycagewins commented 9 years ago

Ok, fixed it without any changes to the library. Simply added android:launchMode="singleInstance" (not sure if singleTask is better...) and removed all the flags in the Intent created when receiving the push notification. The Activity is not recreated if already running. This can be closed.

lukaspili commented 8 years ago

android:launchMode="singleTop" :)