Thursday, 29 October 2020

Jetpack Navigation Component handle navigation controller with Bottom Navigation and it's Back Stack navigation

Hello i am developing one application and i am stuck on navigation in Navigation View.

Example I have activity and inside i have define navigation host controller like below

    setSupportActionBar(mMainToolbar);

    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.homeFragment, R.id.questionBankFragment,
            R.id.testFragment, R.id.dailyHuntFragment,
            R.id.liveClassesFragment)
            .setOpenableLayout(mMainDrawerLayout)
            .build();

    View mNavigationViewHeaderView = mNavigationView.getHeaderView(0);

    mNavHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);

    NavigationUI.setupActionBarWithNavController(this, mNavController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(mNavigationView, mNavController);

    NavigationUI.setupWithNavController(mMainToolbar, mNavController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(mBottomNavigationView, mNavController);

    mNavController.addOnDestinationChangedListener((controller, destination, arguments) -> {
        switch (destination.getId()) {

            case R.id.homeFragment:
            case R.id.questionBankFragment:
                mMainToolbar.setVisibility(View.VISIBLE);
                mBottomNavigationView.setVisibility(View.VISIBLE);
                setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
                setDrawerLocked(false);
                break;


            case R.id.welcomeFragment:
            case R.id.loginFragment:
                mMainToolbar.setVisibility(View.GONE);
                mBottomNavigationView.setVisibility(View.GONE);
                setStatusBarColor(Color.WHITE);
                setDrawerLocked(true);
                break;

        
            case R.id.customModuleFragment:
            case R.id.createCustomModuleOneFragment:
            case R.id.createCustomModuleTwoFragment:
                mBottomNavigationView.setVisibility(View.GONE);
                setDrawerLocked(true);
                break;
            case R.id.logoutFragment:

                new AppSharedPreference(MainActivity.this).clearAllData();

                Intent intent = new Intent(getApplicationContext(), UserAuthanticationActivity.class);
                startActivity(intent);
                finish();
                break;

        }
    });

It is working fine. but now i have to handle navigation on back stack that i not able to understand.

My Question is suppose i am bottom navigation There are 4 Menu. Home, Hunting , Qbank, Exam.

I am selected Qbank. Qbank have 5 fragments A, B, C, D, E. now i have started traverse Fragment A -> B B -> C C -> D

and now i have choice where i have to navigate Back to Direct A like . Current position is D

D - > A

but how i can get back to A and clear stack that i traverse A -> B -> C -> D i want clear this and it's like i want to start again with A.

How I can make possible with Jetpack Navigation Component.

i tried direct navigation or popback stack navigation but it is not work it is gives me error like if i directly navigato to

java.lang.IllegalStateException: View androidx.core.widget.NestedScrollView{98060fe VFED..... ......ID 0,0-0,0} does not have a NavController set
        at androidx.navigation.Navigation.findNavController(Navigation.java:84)
        at app.technotech.koncpt.McqTestFragment$2.onClick(McqTestFragment.java:232)
        at android.view.View.performClick(View.java:7201)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:7170)
        at android.view.View.access$3500(View.java:806)
        at android.view.View$PerformClick.run(View.java:27562)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

I don't have any idea how i can handle this navigation.Pleas help to fix this my solution

Thanks in advance.



from Jetpack Navigation Component handle navigation controller with Bottom Navigation and it's Back Stack navigation

No comments:

Post a Comment