Search code examples
androidandroid-activityback-stack

How to manage Activity stacks?


I have a basic questions and looking for the "best practice" on how to handle that.

When i open my app, i launch Activity A. Users can navigate using the navigation drawer and launch Activity B. From there, they can launch Activity A again, and so on.

This way the backstack is full of duplicate activities. What is the best approach to work around that?

Thanks!


Solution

  • The Best Practice would be to use Fragments if you are using Navigation Drawer, adding it to the one single Activity, However if you want to use Activity and want to avoid duplicate activities in back stack, then you can use different launch Modes of Activity.

    For example you can define launch mode of Activity A as singleTask, this way duplication can be avoided.

    In Manifest for Activity A define

    android:launchMode="singleTask".