Android: Remove activity from history stack January 18th, 2012

In most Android REST-Client applications, the first screen a user sees is either a Login or Registration screen. Once the user logs in, the user is generally taken to a Dashboard screen or to some other Home screen. Now, What do you think will happen if the user presses the back button? Won’t they be taken back to the Login Screen? That is not good design, Is it? The issue is no different even when we show a Blank/Splash screen while automatically logging the user in based on their saved credentials. In fact in the second case it is worse. The user gets to see a Blank screen when they press the back button.

To avoid this behavior, we have to tell android to remove the Login screen from the display/history stack once its job is complete. There are a variety of ways to do this. The easiest way is to give the LoginActivity a “android:noHistory = true” attribute in the manifest file. That instructs Android to remove the given activity from the history stack thereby avoiding the aforementioned behavior altogether.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.activity"
          android:versionCode="1"
          android:versionName="1.0">

  <application android:name="MyApp" android:label="My Application">

    <activity android:name=".LoginActivity" android:noHistory="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>

  </application>
</manifest>

In other similar circumstances, we may want a similar but dynamic behavior where we would like to choose at runtime, if an activity should be removed from the history stack or not. In those cases we can use Intent.FLAG_ACTIVITY_NO_HISTORY intent flag to achieve the same feature dynamically.

There is a cornucopia of other Intent flags available and documented here for our usage pleasure. They may render themselves useful under other circumstances. Have fun learning them all…

12 Responses to “Android: Remove activity from history stack”

  1. [...] jQuery("#errors*").hide(); window.location= data.themeInternalUrl; } }); } http://www.gmarwaha.com – Today, 8:48 [...]

  2. [...] In most Android REST-Client applications, the first screen a user sees is either a Login or Registration screen. Once the user logs in, the user is generally taken to a Dashboard screen or to some other Home screen. Now, What do you think will happen if the user presses the back button? Won’t they be taken back to the Login Screen? That is not good design, Is it?    Mobile Read the original post on DZone… [...]

  3. 3. breast augmentation on January 26th, 2012 at 11:43 am

    Hey dude, I appreciate you for sharing this post. I found the info is very useful for me to remove activity from history stack. Keep it up.

  4. 4. tercüme on February 10th, 2012 at 6:15 pm

    as new innovations appear on informaiton tecnologies we need to learn so many thing everyday to keep up with them one of which is Android technology..

  5. 5. Revathi on February 16th, 2012 at 6:59 am

    Hi….
    thank you so much…its really helped me…

  6. 6. Vinod on February 26th, 2012 at 3:16 pm

    Informative and handy indeed!

  7. 7. blackberry developers on March 19th, 2012 at 7:50 am

    Good piece of script. Does it help to clear the general memory of the Andoid? Or is it only applicable for the activity history? It’d be great to know that.

  8. 8. android gadget on March 26th, 2012 at 5:21 pm

    nice info..!!
    See : http://www.allandroidgadget.com
    For Android Gadget Review Portal
    Thank’s…

  9. 9. bedroom ideas on March 28th, 2012 at 8:40 am

    HI
    Android: Remove activity from history stack
    I’m quite pleased with the information in this post. nice and useful!
    thank you!

  10. 10. Kravvitz on April 25th, 2012 at 10:25 pm

    This is off-topic (I can’t seem to find another way to contact you), but are you aware that other pages on this site have been hacked? Some malicious JavaScript code has been added to the top of some of the pages.

  11. 11. Swathi on August 10th, 2012 at 12:55 pm

    Awesome! This post very informative… Keep them coming up! :)

  12. 12. JavaPins on August 28th, 2012 at 6:40 pm

    Ganesh » Blog Archive » Android: Remove activity from history stack…

    Thank you for submitting this cool story – Trackback from JavaPins…