'I am doing bottom navigationview but my Fragments are stacking on top of each other
I am new in android and I don't know what's up with my code I need help I put 2 or 3 code in same one and somehow works, but only problem I have with this is In Android Studio, I started a new project with bottomNavigationView and now when I run the application in the emulator, the Home Fragment is there by default but then is never replaced when I switch to another fragment. Instead, the new fragments replace each other while still being on top of the first fragment. So if I had 3 fragments, fragment 1 stays stuck in the activity while fragment 1 (duplicate), fragment 2, and fragment 3 all replace each other on top of fragment 1 (original). I don't know how to get rid of the original fragment, or where it is even coming from. I'm new to Android development and learning so I am struggling to find suggestions. Here is the code in my mainactivity:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.Toast;
import com.abc.finalprojectscreen.ui.gallery.GalleryFragment;
import com.abc.finalprojectscreen.ui.home.HomeFragment;
import com.abc.finalprojectscreen.ui.slideshow.SlideshowFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.internal.NavigationMenuView;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
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 com.abc.finalprojectscreen.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener , BottomNavigationView.OnItemSelectedListener {
private AppBarConfiguration mAppBarConfiguration;
NavigationView navigationView;
BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = findViewById(R.id.navigation);
navigation.setOnItemSelectedListener(this);
navigationView = (NavigationView) findViewById(R.id.nav_view);
loadFragment(new home1());
navigationView.setNavigationItemSelectedListener(this);
Fragment fragment = new HomeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment_content_main, fragment);
fragmentTransaction.commit();
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentLayout, fragment)
.commit();
return true;
}
return false;
}
// @Override
// public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Fragment fragment = null;
//
// switch (item.getItemId()) {
// case R.id.home:
// fragment = new home1();
// break;
//
// case R.id.video:
// fragment = new video();
// break;
//
// case R.id.help:
// Intent i =new Intent(MainActivity.this,helpa.class);
// startActivity(i);
// break;
// case R.id.Menu:
// DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
// drawer.openDrawer(GravityCompat.START);
// return true;
// }
//
// return loadFragment(fragment);
//
// }
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
String title = getString(R.string.app_name);
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.home:
fragment = new home1();
loadFragment(fragment);
break;
case R.id.video:
fragment = new video();
loadFragment(fragment);
break;
case R.id.help:
Intent i =new Intent(MainActivity.this,helpa.class);
startActivity(i);
break;
case R.id.Menu:
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.START);
return true;
}
if (id == R.id.nav_home) {
fragment = new HomeFragment();
title = "Home";
} else if (id == R.id.help_app) {
Intent i =new Intent(MainActivity.this,helpa.class);
startActivity(i);
} else if (id == R.id.more_apps) {
fragment = new SlideshowFragment();
title = "Feedback";
} else if (id == R.id.rate_app) {
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName());
Intent i = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(i);
} catch (Exception e) {
Toast.makeText(this, "Unable to open\n" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else if (id == R.id.share_app) {
try {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Wisdom locator");
i.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName());
startActivity(Intent.createChooser(i, "Share With"));
} catch (Exception e) {
Toast.makeText(this, "Unable to open\n" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentLayout, fragment);
fragmentTransaction.commit();
setTitle(title);
}
return true;
}
}
thank for your Ans so mix both of them in same one so it is little confusing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
