Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
57 views
in Technique[技术] by (71.8m points)

android - Can't access fragment after clearing it from back stack

I have two flows that can happen in my app: A1->A2->B or A1->B, A and B being different activities, 1 and 2 different fragments. When in B, there's no reason to go back to A2, so I'm using the following code to clear the stack of fragments before starting activity B (startControle()) from A2:

requireFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
(activity as MainActivity?)?.startControle()

The problem is that when I go back to A1 and try to go to A2, the app crashes

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ufrj.projetointegrado, PID: 23181
    java.lang.IllegalArgumentException: Navigation action/destination com.ufrj.projetointegrado:id/action_inicio_to_btOff cannot be found from the current destination Destination(com.ufrj.projetointegrado:id/btOff) label=btOff class=com.ufrj.projetointegrado.btOff
        at androidx.navigation.NavController.navigate(NavController.java:938)
        at androidx.navigation.NavController.navigate(NavController.java:875)
        at androidx.navigation.NavController.navigate(NavController.java:861)
        at androidx.navigation.NavController.navigate(NavController.java:849)
        at com.ufrj.projetointegrado.inicio$onCreateView$1.onClick(inicio.kt:31)
        at android.view.View.performClick(View.java:6600)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:6577)
        at android.view.View.access$3100(View.java:781)
        at android.view.View$PerformClick.run(View.java:25917)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:215)
        at android.app.ActivityThread.main(ActivityThread.java:6939)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

Anyone knows what am I doing wrong?

EDIT

I also tried this way, but it behaves the same way:

val navController = this.findNavController()
navController.popBackStack(R.id.inicio, true);
(activity as MainActivity?)?.startControle()
question from:https://stackoverflow.com/questions/65947134/cant-access-fragment-after-clearing-it-from-back-stack

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly, when using Navigation, you must always use the NavController APIs - specifically, use navController.popBackStack() and not the FragmentManager APIs directly.

Secondly, passing true to popBackStack() means an inclusive pop - i.e., also pop the destination you pass in. If you only want to pop A2 while you're on A2, you could

A) Use popBackStack() (which pops just the topmost destination) B) Use popBackStack() with the ID of A2 and an inclusive of true C) Use popBackStack() with the ID of A1 and an inclusive of false

Thirdly, while calling popBackStack() and then calling startActivity() manually is totally fine, you can also use a combination of popUpTo and activity destinations to pop your destination and navigate to an activity as a single operation.

<activity
    android:id="@+id/activity_b"
    android:name="com.example.ActivityB" />

Then add an action to navigate from A2 to B:

<action
    android:id="@+id/a2_to_b"
    app:popUpTo="@+id/inicio"
    app:destination="@+id/activity_b" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...