Wednesday, February 20, 2013

How to show/prompt login every time user returns or open the application

Today, I wanted to know how to show the application login when a user opens or returns to the application every-time. Most difficult part about this is how to get rid of activities popping out from the stack history.

This is how I did it.

I marked the main activity as



  <activity
            android:name=".ui.MainSettingsActivity" android:label="@string/app_name" android:launchMode="singleTop" android:clearTaskOnLaunch="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



and prompt for password in `onCreate` and `onNewIntent` in the Activity.java worked like a charm

This way, if the user open a new instance or use a existing from from stack history we can popup for the password.

Key part here is "singleTop".

If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to itsonNewIntent() method, rather than creating a new instance of the activity.