如何实现底部导航栏交易不碎片化,而是 Activity class?
How to Implementing bottom navigation bar transaction not to fragment , however to Activity class?
如果我单击底部导航项,如何过渡到 activity 而不是片段?
setContentView(R.layout.activity_main);
fm.beginTransaction().add(R.id.main_container,fragment3).hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container,fragment2).hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1).commit(); //this is fragment
//this is implementing fragment
final BottomNavigationView bottomNavigationView = findViewById(R.id.nav_bawah);
bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
fm.beginTransaction().hide(active).show(fragment1).commit();
active=fragment1;
break;
case R.id.navigation_challange:
fm.beginTransaction().hide(active).show(fragment2).commit();
active=fragment2;
break;
case R.id.navigation_chat:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
break;
}
return true;
}
});
使用Intent
class开始新的activity。
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.item_related_to_activity:
Intent intent = new Intent(requireActivity(), YourActivity.class);
startActivity(intent);
break;
// other cases ...
}
return true;
}
});
如果我单击底部导航项,如何过渡到 activity 而不是片段?
setContentView(R.layout.activity_main);
fm.beginTransaction().add(R.id.main_container,fragment3).hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container,fragment2).hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1).commit(); //this is fragment
//this is implementing fragment
final BottomNavigationView bottomNavigationView = findViewById(R.id.nav_bawah);
bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
fm.beginTransaction().hide(active).show(fragment1).commit();
active=fragment1;
break;
case R.id.navigation_challange:
fm.beginTransaction().hide(active).show(fragment2).commit();
active=fragment2;
break;
case R.id.navigation_chat:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
break;
}
return true;
}
});
使用Intent
class开始新的activity。
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.item_related_to_activity:
Intent intent = new Intent(requireActivity(), YourActivity.class);
startActivity(intent);
break;
// other cases ...
}
return true;
}
});