Closing All Activities and Launching Any Specific Activity

Often times, most apps have an option where all the activities of the current app are closed and any new specific activity is launched. For example, on logging out of the application, all the activities are closed and login activity is started to allow user to make any new session.



This can be done using few lines code with power of intents like this:

Kotlin

  val i = Intent(applicationContext, SplashActivity::class.java)        // Specify any activity here e.g. home or splash or login etc
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  i.putExtra("EXIT", true)
  startActivity(i)
  finish()

Java

  Intent i = new Intent(applicationContext, SplashActivity.java);        // Specify any activity here e.g. home or splash or login etc
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  i.putExtra("EXIT", true);
  startActivity(i);
  finish();


If you liked this article, you can read my new articles below:


profile card
Wajahat Karim
🌍 Making the world a better place, one app at a time.
🔥 Google Developer Expert (GDE) in Android . 📱 Professional Android Developer with ~10 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.
Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan