Learn Pain Less

HomeOur TeamContact
Android
How to clear navigation stack after navigating to another fragment in Android
Pawneshwer Gupta
Pawneshwer Gupta
January 02, 2020
1 min
How to clear navigation stack after navigating to another fragment in Android

clear navigation stack after navigating to another fragment

In this tutorial we will discuss about Android Navigation library which is part of Jetpack. This library help us in managing navigation in our android app by creating single Activity, Navigation graph etc. But as you start using Android Navigation you may face few problems. So we will discuss about one problem in this post.

So if we want to navigate to another fragment and we want to clear stack (remove all fragments which were opened previously) then we can tweak this by simply navigating to root of Navigation with the help of Navigation graph and then navigate to our destination fragment. Fortunately we don’t need to this all stuff manually. We can do it easily with following method.

In your Navigation Graph add new Action from Fragment1 to Fragment2 and inside that action add following properties popUpTp=“Navigation_Graph_Id” and popUpToInclusive=“true” and rebuild your project. After rebuilding project navigate to that newly created direction and that’s it.

clear navigation stack after navigating to another fragment in Android
clear navigation stack after navigating to another fragment in Android

Here is complete Example:

Code of res/navigation/main_graph.xml

After pasting above code in Navigation Graph, rebuild your project. Now do the following in current fragment

Now you will redirect from SecondFragment to ThirdFragment and FirstFragment and SecondFragment will be cleared from stack.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

Jetpacknavigationfeatured

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

2 API requests parallelly using Retrofit2 and RxJava
2 API requests parallelly using Retrofit2 and RxJava
May 07, 2018
1 min
Why we have to think about state in flutter (Beginner's question)
Flutter
Why we have to think about state in flutter (Beginner's question)
August 08, 2020
2 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media