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


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

android - Moving from fragment to fragment managed by Navigation Drawer

I took Navigation Drawer from Android Studio template. Everything works fine, but there are several problems associated with not understanding this component. I'm almost new, came from Delphi.

I need to come up with a transition between fragments from the Navigation Drawer, in the fragment itself, by pressing a button. That is, fragment1: buttonclick> fragment2.The usual Intent does not work, and the FragmentManager, as I understand it, only changes the xml files, and I need to initialize the class. All rummaged, found nothing like that.


package esport.inyourlife.insport;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import esport.inyourlife.insport.ui.slideshow.ProfileFragment;

public class HomeNav extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private TextView uMail, uName;
    private FirebaseAuth mAuth;
    private final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = findViewById(R.id.toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Позже уберу эту штуку, при оптимизаций приложений", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_tournament, R.id.nav_profile)
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        uMail = findViewById(R.id.uMail);
        uName = findViewById(R.id.uName);
        /*Intent i = getIntent();
        if (i != null)
            Toast.makeText(this, "Ошибка получения емайл", Toast.LENGTH_SHORT).show();
        FirebaseAuth.AuthStateListener stateListener = new FirebaseAuth.AuthStateListener() {
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (user != null)
                    if (!user.getDisplayName().equals(null))

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_nav, menu);
        return true;
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.signout :
            startActivity(new Intent(this, LoadFrame.class));
            return true;
            case R.id.test :
                Toast.makeText(this, "Отсутсвует метод", Toast.LENGTH_SHORT).show();
                return true;
        return super.onOptionsItemSelected(item);

    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    public void Profile(View v)
        Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getApplicationContext(), ProfileFragment.class));

    protected void onStart() {

    // public void SignOut(View view) { mAuth.signOut(); }


Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To navigate from current fragment to next fragment, use this code in your current fragment:

Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.nextFragment, null));

To navigate with a transition animation, set the id of the fragment you want to navigate to (nextFragment), to the id of the action containing the fragment transition.

Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_currentFragment_to_nextFragment, null));

If you already have an onClickListener set for the button, place this inside it instead:


Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share