?

Log in

Previous Entry | Next Entry

We got back to Activity Launch Modes because of the way of initialization of WAMS notifications. The WAMS NofificationHandler requires Context in its registration handlers and we have a little choice, but provide the some (usually root main) activity to it as the desired context. The problem with approach is re-creating the activity that started with ‘standard’ launch mode. Because this mode used by default, the activity for which the launch mode is not declared explicitly, will be launched under its conditions. The conditions of ‘standard’ launch mode imply the system to always create a new instance of the activity in the target stack and (probably) destroy the previously launched instance. This, in turn, will cause the NotificatinHandler be unregistered (on destroy) and registered again (on create). Obviously, this is not the desired behavior.
In order to prevent the repeatable registration and un-registration events, our main activity will be rather declared to start with ‘singleTop’ launch mode.
Under this mode, if an instance of the activity exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, no new instance of such activity is created.
This is much better for NotificationHandler. Under these conditions, it will be registered on create and continue living surviving child activities launches and closes. The only way to un-register the NofificationHandler inside ‘singleTop’ activity will be  to actually destroy it. But indeed, such destructions is a logical end of the handler. For entire life of the application (as long as its root activity remains existing on the task stack) the handler is alive!

‘Standard’ Launch Mode
With 'standard' (default), launch mode when the execution passed to the child activity, main activity is stopped (onStop) and not removed from the stack.
When navigated Up from the child back to such activity, it is destroyed (onDestroy) and created again (onCreate).
To enable Up navigation, the child activity should specify its parent activity (preferable in manifest file) and call get[Support]ActionBar().setDisplayHomeAsUpEnabled(true);  from within its onCreate() handler.
So long as parent activity is specified for the child, pressing on the left-facing caret is handled automatically as an Up navigation. ‘Automatically’ means that Activity class’ handler executes the code similar to the following:
Obviously, no one can prevent you to include this code to the onOptionsItemSelected() handled and thus avoid call to super.onOptionsItemSelected(item).  Anyway, this click causes the child activity to receive onOptionsItemSelected() call as usual event from ActionBar. The ID for this action is android.R.id.home.
Thus, 'standard' launch mode causes to system always to create a new instance of the activity in the target stack.
There are may be several instances of such activity on the stack.
‘singleTop’ Launch Mode
Under 'singleTop' launch mode the new instance of the activity is not created when navigating up from the child, unless this activity is not on the top the task stack. When it is on the top, the new intents are delivered to such activity thru onNewIntent(), then the activity gets started (onStart).

Tags: