androidx.navigation.fragment.NavHostFragment Java Examples

The following examples show how to use androidx.navigation.fragment.NavHostFragment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UsernameEditFragment.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private void onEvent(@NonNull UsernameEditViewModel.Event event) {
  switch (event) {
    case SUBMIT_SUCCESS:
      Toast.makeText(requireContext(), R.string.UsernameEditFragment_successfully_set_username, Toast.LENGTH_SHORT).show();
      NavHostFragment.findNavController(this).popBackStack();
      break;
    case SUBMIT_FAIL_TAKEN:
      Toast.makeText(requireContext(), R.string.UsernameEditFragment_this_username_is_taken, Toast.LENGTH_SHORT).show();
      break;
    case SUBMIT_FAIL_INVALID:
      Toast.makeText(requireContext(), R.string.UsernameEditFragment_username_is_invalid, Toast.LENGTH_SHORT).show();
      break;
    case DELETE_SUCCESS:
      Toast.makeText(requireContext(), R.string.UsernameEditFragment_successfully_removed_username, Toast.LENGTH_SHORT).show();
      NavHostFragment.findNavController(this).popBackStack();
      break;
    case NETWORK_FAILURE:
      Toast.makeText(requireContext(), R.string.UsernameEditFragment_encountered_a_network_error, Toast.LENGTH_SHORT).show();
      break;
  }
}
 
Example #2
Source File: DynamicNavigationActivity.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dynamic_navigation_activity);

    if (savedInstanceState == null) {
        final NavHostFragment finalHost = new NavHostFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.nav_host, finalHost)
                .setPrimaryNavigationFragment(finalHost)
                .commit();
    }
}
 
Example #3
Source File: MainActivity.java    From ground-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  // Make sure this is before calling super.onCreate()
  setTheme(R.style.AppTheme);
  super.onCreate(savedInstanceState);

  MainActBinding binding = MainActBinding.inflate(getLayoutInflater());

  setContentView(binding.getRoot());

  navHostFragment =
      (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);

  viewModel = viewModelFactory.get(this, MainViewModel.class);

  activityStreams
      .getActivityRequests()
      .as(autoDisposable(this))
      .subscribe(callback -> callback.accept(this));

  // TODO: Remove once we switch to persisted auth tokens / multiple offline users.
  authenticationManager
      .getSignInState()
      .as(autoDisposable(this))
      .subscribe(this::onSignInStateChange);

  navigator.getNavigateRequests().as(autoDisposable(this)).subscribe(this::onNavigate);
  navigator.getNavigateUpRequests().as(autoDisposable(this)).subscribe(__ -> navigateUp());
}
 
Example #4
Source File: MainActivity.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
protected void initView() {
    NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.main_host_fragment);
    if (navHostFragment != null) {
        NavigationUI.setupWithNavController(mNavigation, navHostFragment.getNavController());
    }
}
 
Example #5
Source File: NavigationActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_activity);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    NavHostFragment host = (NavHostFragment) getSupportFragmentManager()
            .findFragmentById(R.id.my_nav_host_fragment);

    if (host != null) {
        NavController navController = host.getNavController();
        mDrawerLayout = findViewById(R.id.drawer_layout);
        NavigationUI.setupActionBarWithNavController(this, navController, mDrawerLayout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        if (navigationView != null) {
            NavigationUI.setupWithNavController(navigationView, navController);
        }
        BottomNavigationView bottomNavView = findViewById(R.id.bottom_nav_view);
        if (bottomNavView != null) {
            NavigationUI.setupWithNavController(bottomNavView, navController);
        }
        navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
            @Override
            public void onNavigated(@NonNull NavController controller,
                    @NonNull NavDestination destination) {
                String dest;
                try {
                    dest = getResources().getResourceName(destination.getId());
                } catch (Resources.NotFoundException e) {
                    dest = Integer.toString(destination.getId());
                }
                Toast.makeText(NavigationActivity.this, "Navigated to "
                        + dest,
                        Toast.LENGTH_SHORT).show();
                Log.d("NavigationActivity", "Navigated to " + dest);
            }
        });
    }
}
 
Example #6
Source File: ImmediateNavigationActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    NavHostFragment.findNavController(this).navigate(R.id.deep_link_test);
}