0

SettingsLandingActivity holds multiple fragments and handles the transition in between them using NavController. When user enters the activity, it shows SettingsLandingFragment, as it is the default fragment.

Then user clicks on GeneralSettings shortcut, so it triggers an intent for GeneralSettingsFragment, it again uses the following code to switch to GeneralSettingsFragment. So, GeneralSettingsFragment is resumed and SettingLandingFragment gets onViewDestroyed.

Then user directly switches to another activity, GeneralSettingsFragment gets onPause. Then user clicks on Settings button, then the sequence of events is-

GeneralSettingsFragment gets onStart.

SettingsLandingActivity gets onNewIntent

SettingLandingFragment gets onViewCreate, onStart and OnResume

As GeneralSettingsFragment gets onStart, that fragment appears for a short moment as a flicker, then SettingsLandingFragment is shown. The expectation is to show SettingLandingFragment without any showing previous screen. It is also a requirement to always switch to SettingsLandingFragment when Settings button is clicked.

The following method is called from onNewIntent of activity to handle navigations and the parameter clearBackStack is passed as TRUE. How to avoid the flicker of previous fragment when a different fragment is requested from different activity? Kindly help me to solve this issue.

    private void navigateToFragments(int actionNavigate,
                                 boolean clearBackStack,
                                 Bundle bundle) {
    
    NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
            .findFragmentById(R.id.nav_host_fragment);
    if (navHostFragment != null) {
        navController = navHostFragment.getNavController();
        NavOptions navOptions;
        if (clearBackStack) {
            navOptions = new NavOptions.Builder()
                    .setPopUpTo(navController.getGraph().getId(), true).build();
            Logger.d(TAG, "Pop and clearing the all back stack");
        } else {
            navOptions = new NavOptions.Builder()
                    .setPopUpTo(actionNavigate, true).build();
            Logger.d(TAG, "Pop and clearing the all " +
                    "intervening destinations up to current navigate destination.");
        }
        Logger.d(TAG, " Navigate to a destination from the current navigation graph");
        navController.navigate(actionNavigate, bundle, navOptions);
    } else {
        Logger.e(TAG, "NavHostFragment is null");
    }
}

1 Answer 1

0

You've tried navigating through the stack of elements. When the navigation graph is created, each fragment has a specific element item.

Kotlin example

val nv: NavigationView = (context as SidebarActivity).findViewById(R.id.nav_view)
val item = nv.menu.getItem(3) //Item of fragment target
val navController = (context as SidebarActivity).findNavController(R.id.nav_host)
onNavDestinationSelected(item, navController)
Sign up to request clarification or add additional context in comments.

1 Comment

is it possible to set the navgraph start destination programmatically from activity class, so that the one we set in activity class is created, not the one set in nav graph layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.