Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEyal Iaroslavitz
Courses Plus Student 532 PointsWhy use FLAG_ACTIVITY_CLEAR_TASK when using FLAG_ACTIVITY_NEW_TASK (from Navigation with back and Up video)
I've just seen the Navigation with back and Up video and I don't understand why one need to add FLAG_ACTIVITY_CLEAR_TASK and not just using the FLAG_ACTIVITY_NEW_TASK only? after all when using a new task the starting activity is the new root the the new q....
Thank u in advace
1 Answer
Kevin Haube
12,265 PointsFLAG_ACTIVITY_CLEAR_TASK
will cause any existing task that would be associated with the activity to be cleared before the activity is started. This way, when you load that FLAG_ACTIVITY_NEW_TASK
, and you hit the back button, you won't end up back at a login or sign up screen. That'd be a little awkward for our users if they were already logged in and hit it by accident.
Think of it as a stack of papers. Log in on top, main activity on the bottom. When we call FLAG_ACTIVITY_CLEAR_TASK
, we remove the log in paper. Now we're looking at the main activity paper. But if we turn the page back, we don't see the log in paper! :)
If you'd like, you can read more about it under the Intent section of the Android Developer references!
Eyal Iaroslavitz
Courses Plus Student 532 PointsEyal Iaroslavitz
Courses Plus Student 532 PointsThank u for ur answer. I understand the stack of papers analogy but I still dont understand why we need both flags and not one of them only. Using CLEAR_TASK flag will clear the old task obviously the new activity will be the root of the task and vice verse if we use only the NEW_TASK the new activity will be the new root of the stack so who cares about the old stack....
Am I missing something here?
Kevin Haube
12,265 PointsKevin Haube
12,265 PointsIn the Tasks and Back Stack section of the Android Developer guide, Figure 2 provides a visualization of what happens if we don't clear a task. Basically, it'll be running in the background, waiting to resume. If we hit the end of Task B's back stack, Task A will be next in line to resume when you hit the back arrow.
Clearing the task eliminates it, and sets our new Task, the one starting on MainActivity, as Task A, so at the end of it's Back Stack, it closes out to the Home Screen. :)
Eyal Iaroslavitz
Courses Plus Student 532 PointsEyal Iaroslavitz
Courses Plus Student 532 PointsI see. thank you!!