문제

I have an application which replaces the fragment in a container when the user navigates. (Based initially on the method android studio creates if you start a new project with spinner navigation selected)

I want the user to be able to press the back button to go back through previous pages, but not have to press back a million times to exit the app.

I can add the fragments to the backstack but it means there can be a huge stack OR I have to find the last instance manually and clear part of the stack. If I use activities I can just launch my intents with the SINGLE_TOP flag. Is there an equivalent method I can use when replacing fragments?

도움이 되었습니까?

해결책 2

It appears that there is no equivalent.

The best option currently is to call remove(fragment) as part of your transaction, although the downside to that is that it seems to pop anything that's above it in the stack

다른 팁

There is a new Android jetpack component named Android navigation component that will help you to get the desired objective. Here is a good tutorial on it where you will also find the use of SingleTop. This article describes a line like which is related to your use case.

The app:launchSingleTop is used whether navigation action should launch as single-top (i.e., there will be at most one copy of a given destination on the top of the back stack). This functions similarly to how android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP works with activities.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top