Android

How To Clear Activity Back Stack On Logout

While developing an application with feature logout, we might have come across a problem of session management. Even if we managed to solve the authentication, we have to deal with an another problem that is page history clearing.

When each user logouts out from application, we have to clear the history of activities that he has created during his session.

In android application, we know that all the pages in the user interfaces are basically Activities. And here history is Back Stack.

Suppose, i want to make an application with login page and home page. What i would do is i would create login page and set it as the landing page of my application and create one more activity called home page. Home page will be the landing page after the successful authentication in the login page.

I will show the code for the above scenario,

First Create Login Activity.

LoginActivity.kt
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class LoginActivity:AppCompatActivity() {

  protected fun onCreate(savedInstanceState:Bundle) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    findViewById(R.id.btn_demo_home).setOnClickListener(object:View.OnClickListener() {

      fun onClick(v:View) {
        goHome()
      }
    })
  }

  private fun goHome() {

    val intent = Intent(getApplicationContext(), HomeActivity::class.java)
    startActivity(intent)

  }
}

Create another activity called HomeActivity

HomeActivity.kt
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class HomeActivity:AppCompatActivity() {

  protected fun onCreate(savedInstanceState:Bundle) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)
    findViewById(R.id.btn_logout).setOnClickListener(object:View.OnClickListener() {

      fun onClick(v:View) {
        doLogout()
      }

    })

  }

  //This method will clear Activity Backstack
  private fun doLogout() {

    val intent = Intent(this, LoginActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)
    finish() // finish the current activity

  }
}

The “doLogout()” method of the HomeActivity do the work of clearing backstack of Activities.

Finally, set Login Activity as the landing page of the app.

<!--?xml version="1.0" encoding="utf-8"?-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.devdeeds.demologout">

    <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/AppTheme">

        <activity android:name=".HomeActivity">        
        <activity android:name=".LoginActivity">

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

        </activity>
    </activity>
</application>

</manifest>

That’s it.

Let’s Analyse the core code block

private fun doLogout() {
  val intent = Intent(this, LoginActivity::class.java)
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or 
  Intent.FLAG_ACTIVITY_NEW_TASK)
  startActivity(intent)
  finish() // finish the current activity
}

The above code actually do nothing but starts the Login Activity again with 2 Flags.

Imagine a backstack as stack of activity added one over the other in LIFO manner.

First one, FLAG_ACTIVITY_CLEAR_TOP tells system to clear all the activities on the top of LoginActivity in the backstack if it is already exists in backstack. In our case, there will be always an instance in the backstack because LoginActivity is our landing Activity of the app.

Second one, FLAG_ACTIVITY_NEW_TASK tells the system, create a new Task and set LoginActivity as the first activity in the Task’s backstack.

A Task is nothing but a atomic group of activities.

Reference: https://developer.android.com/guide/components/activities/tasks-and-back-stack

About author

Geethu is a teacher by profession, but she likes to research new things and is also interested in writing. Devdeeds is her blog, where she writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *