Android: Remove activity from history stack

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…

Ganeshji Marwaha

I spend my days as the Director of Technology for Mobility practice and help my clients design enterprise and consumer mobile strategies. Mobile Payments, Digital Wallet and Tokenization technologies are my areas of specialization

  • Pingback: Android: Remove activity from history stack | Ganesh | Mostly Programming | Scoop.it()

  • Pingback: Android: Remove activity from history stack | Mobile | Syngu()

  • http://www.dramjadi.com/ breast augmentation

    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.

  • http://www.elsatercume.com tercüme

    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..

  • Revathi

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

  • Vinod

    Informative and handy indeed!

  • http://www.intellectsoft.net/blackberry_application_development.html blackberry developers

    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.

  • http://www.allandroidgadget.com android gadget

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

  • http://bedroom-a.com bedroom ideas

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

  • http://www.dynamicsitesolutions.com/ Kravvitz

    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.

  • http://femgeekz.blogspot.in/ Swathi

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

  • Pingback: JavaPins()