Hi i'm making an app where i'm using fragments with bottom navigation however when i click on home button it doesnt load home fragment instead it loads the second fragment and moreover when i start the app home fragment doesnt load by default i know it may sound naive but i'm having this problem plaese if someone may guide me
my code for fragment navigation
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
FragmentManager fragmentManager;
Fragment fragment;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//BottomNavigationView b=findViewById(R.id.bottom_nav);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.frame_container)!=null){
if(savedInstanceState!=null){
return;
}
/*HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment,null);
transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
//transaction.commit();
}
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
//final SeekBar sb = (SeekBar) findViewById(R.id.sb);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
});
}
private void loadFragment(Fragment fragment) {
// HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment,null);
transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
// load fragment
//HomeFragment homeFragment = new HomeFragment();
}
}
You just forgot to return true; inside case R.id.navigation_home: check it
Try this
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
});
Related
Hello is there any way to keep a fragment running while navigating with the navigationbar?
here is my code
public class DashActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
FirebaseAuth firebaseAuth;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
actionBar = getSupportActionBar();
actionBar.setTitle("Profile");
firebaseAuth = FirebaseAuth.getInstance();
BottomNavigationView navigationView = findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(selectedListener);
actionBar.setTitle("Profile");
ProfilFragment fragment2 = new ProfilFragment();
FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.replace(R.id.content, fragment2, "");
ft2.commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuitem) {
switch (menuitem.getItemId()) {
case R.id.nav_map:
actionBar.setTitle("Map");
MapFragment fragment1 = new MapFragment();
FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
ft1.replace(R.id.content, fragment1, "");
ft1.commit();
return true;
case R.id.nav_profile:
actionBar.setTitle("Profile");
ProfilFragment fragment2 = new ProfilFragment();
FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.replace(R.id.content, fragment2, "");
ft2.commit();
return true;
case R.id.nav_activity:
actionBar.setTitle("Activity");
ActivityFragment fragment3 = new ActivityFragment();
FragmentTransaction ft3 = getSupportFragmentManager().beginTransaction();
ft3.replace(R.id.content, fragment3, "");
ft3.commit();
return true;
}
return false;
}
};
private void checkUserStatus() {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
} else {
startActivity(new Intent(DashActivity.this, MainActivity.class));
finish();
}
}
#Override
protected void onStart() {
checkUserStatus();
super.onStart();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
return false;
}
}
i have three fragment and i want them to stay running while the application is open
i have to open bottom navigation fragment from one activity so how can i open to this?
So how to pass Flag and open 4th number of bottomnavigation fragment open??
val profileFragment = ProfileFragment()
profileFragment.show(supportFragmentManager, profileFragment.tag)
val profileFragment = ProfileFragment()
val navController = findNavController(R.id.btm_home)
navController.navigate(R.id.btm_profile)
val manager: FragmentManager = fragmentManager
val transaction: FragmentTransaction = manager.beginTransaction()
transaction.add(R.id.container, , YOUR_FRAGMENT_STRING_TAG)
transaction.addToBackStack(null)
transaction.commit()
try this code
public class MainActivity extends AppCompatActivity {
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new DashboardFragment();
final Fragment fragment3 = new NotificationsFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.navigation_dashboard:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.navigation_notifications:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
}
return false;
}
};
I have made the code to do that it loads fragment on clicking the items in the bottom navigation but when I click on it they don't work. I tried many times to check whats the problem even though Android Studio couldn't find it bu the app isn't working. I don't know whats the issue.
My main activity -
public class MainActivity extends AppCompatActivity {
BottomNavigationView btm_view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Chats());
fragmentTransaction.commit();
btm_view = findViewById(R.id.bottom_view);
btm_view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.camera) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Camera());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.chats) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Chats());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.status) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Status());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.calls) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Calls());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.settings) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Settings());
fragmentTransaction.commit();
}
return false;
}
});
}
}
one of my fragments -
public class Chats extends Fragment {
public Chats() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_chats, container, false);
}
}
Try this way
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.chats:
openFragment(Chat.newInstance());
return true;
case R.id.camera:
openFragment(Camera.newInstance());
return true;
case R.id.status:
openFragment(Status.newInstance("", ""));
return true;
}
return false;
}
};
At first use replace(R.id.container, fragment) instead of add.
Optimise Code
Utilise setOnNavigationItemSelectedListener. Try this way
Set a listener that will be notified when a bottom navigation item is
selected.
btm_view.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
openFragment(Camera.newInstance());
}
public void openFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.camera:
openFragment(Camera.newInstance());
return true;
case R.id.chats:
openFragment(Chats.newInstance());
return true;
case R.id.status:
openFragment(Status.newInstance());
return true;
case R.id.calls:
openFragment(Calls.newInstance());
return true;
case R.id.settings:
openFragment(Settings.newInstance());
return true;
}
return false;
}
};
}
Your Chats Fragment will be
public class Chats extends Fragment {
public Chats() {
// Required empty public constructor
}
public static Chats newInstance() {
Chats fragment = new Chats();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_chats, container, false);
}
}
This is my code I have created a bottom navigation view and I am connected the items to fragment separately. When I click the the navigation item more time the fragment load again again.
I don't know what is the problem here.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment SelectedFragment=null;
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.commit();
switch (item.getItemId()) {
case R.id.navigation_home:
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
case R.id.navigation_dashboard:
HomeFragment1 fragment1 = new HomeFragment1();
ft.replace(R.id.fragment_container, fragment1);
return true;
case R.id.navigation_notifications:
setTitle("Safe11");
HomeFragment2 fragment2 = new HomeFragment2();
ft.replace(R.id.fragment_container, fragment2);
return true;
case R.id.navigation_notification:
setTitle("More");
HomeFragment3 fragment3 = new HomeFragment3();
ft.replace(R.id.fragment_container, fragment3);
return true;
}
return false;
}
};
}
Try to change your code like this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.menu_home:
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction.replace(R.id.frameContentPlace, homeFragment, "HomeFragment").addToBackStack("HomeFragment");
break;
case R.id.menu_profile:
HomeFragment1 fragment1 = new HomeFragment1();
fragmentTransaction.replace(R.id.frameContentPlace, fragment1).addToBackStack("HomeFragment1");
break;
case R.id.menu_bookings:
HomeFragment2 fragment2 = new HomeFragment2();
fragmentTransaction.replace(R.id.frameContentPlace, fragment2).addToBackStack("HomeFragment2");
break;
}
fragmentTransaction.commit();
return true;
}
When you adding fragment use tag. So write
ft.replace(R.id.fragment_container, fragment, "MyFragment);
instead of
ft.replace(R.id.fragment_container, fragment);
And check if this fragment is visible for not loading this fragment again
switch (item.getItemId())
{
case R.id.navigation_home:
HomeFragment fragment = (HomeFragment)getFragmentManager().findFragmentByTag("MyFragment");
if (fragment != null && !fragment.isVisible())
{
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
}
}
How can I use onBackPressed in BottonNavigationView to select an item? For example, if I press item 2 I go to fragment number 2, but when I push back, I go back to fragment number 1 and item 2 is still selected, item 1 is not selected which is the one corresponding to fragment 1 ...
This is my code
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
}
UPDATE
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, new MapFragment()).commit();
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomViewBar);
bottomNavigationViewEx.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
bottomNavigationViewEx.setSelectedItemId(R.id.mapa);
MenuItem item = bottomNavigationViewEx.getMenu().getItem(0);
if (!item.isChecked()) item.setChecked(true);
}
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
#Override
public void onBackPressed () {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment (mCurrentItem);
} else {
super.onBackPressed();
}
}
private void showFragment(int currentItem) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
transaction.replace(R.id.content, new TaxiFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 2:
transaction.replace(R.id.content, new HelpFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 3:
transaction.replace(R.id.content, new GoFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
}
}
}
I have this now, but I don't know if is correct
Keep a variable to denote which Fragment is your current. Inside onBackPressed, check what is the current Fragment, and decide where do you want to navigate to.
#Override
public void onBackPressed() {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment(mCurrentItem);
} else {
super.onBackPressed();
}
}
Your showFragment(mCurrentItem) can show different page based on the value, or however you'd like it to.
private void showFragment(int currentItem) {
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
...
}
}
Then inside your showFragment(currentItem) method, add this code to select the item on BottomNavigationView:
MenuItem item = mBottomNavigationView.getMenu().getItem(mCurrentItem);
if (!item.isChecked()) item.setChecked(true);
Solve my problem by adding this line inside the class of my corresponding fragment in the onCreateView method, so when pressing back the item is selected.
BottomNavigationViewEx navigation =
getActivity().findViewById(R.id.YourNavigationId);
Menu drawer_menu = navigation.getMenu();
MenuItem menuItem;
menuItem = drawer_menu.findItem(R.id.YourItemId);
if(!menuItem.isChecked())
{
menuItem.setChecked(true);
}