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.
MainActivity:
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();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_nav);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(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)
.setDrawerLayout(drawer)
.build();
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)
{
uMail.setText(i.getStringExtra("email"));
}
else
{
Toast.makeText(this, "Ошибка получения емайл", Toast.LENGTH_SHORT).show();
}*/
/*
FirebaseAuth.AuthStateListener stateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (user != null)
{
mAuth.removeAuthStateListener(this);
uMail.setText(user.getEmail());
if (!user.getDisplayName().equals(null))
{
uName.setText(user.getDisplayName());
}
}
}
};
mAuth.addAuthStateListener(stateListener);*/
}
@Override
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;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.signout :
mAuth.signOut();
startActivity(new Intent(this, LoadFrame.class));
finish();
return true;
case R.id.test :
Toast.makeText(this, "Отсутсвует метод", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
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));
}
@Override
protected void onStart() {
super.onStart();
}
// public void SignOut(View view) { mAuth.signOut(); }
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…