Reputation: 185
I'm facing a quite frustrating bug in my app, let me explain: I have a MainActivity with an appBar containing 3 tabs and each tab displays a Fragment. The problem comes when I try to lunch another Fragment and make it replace all the screen. The new Fragment is displayed on the replaced one and the two are visible.
Here is my code:
The MainActivity:
public class MainActivity extends AppCompatActivity {
public static Model model;
public static Controller controller;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private static final int PERMISSION_GALERIE = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
this.model = new Model();
this.controller = new Controller(model,this);
final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager)findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.settings,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.db_setting) {
getFragmentManager().beginTransaction().replace(R.id.mainLayout, new DBSettingFragment()).commit();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode==PERMISSION_GALERIE){
if(grantResults.length==1 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
Snackbar.make(getCurrentFocus(),"Permission accordée",Snackbar.LENGTH_SHORT);
}
}
else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
Fragment menusManagementFragment = new MenusManagementFragment();
switch (position) {
case 0:
return menusManagementFragment;
case 1:
OrdersManagementFragment ordersManagementFragment = new OrdersManagementFragment();
return ordersManagementFragment;
case 2:
DeliversManagementFragment deliversManagementFragment = new DeliversManagementFragment();
return deliversManagementFragment;
default: return menusManagementFragment;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Gestion des menus";
case 1:
return "Gestion des commandes";
case 2:
return "Gestiond des livreurs";
}
return null;
}
}}
The main_layout:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
And in DeliversManagementFragment I do:
deliverStatsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction().replace(R.id.mainLayout,new DeliverStatsFragment()).commit();
}
});
If someone could help that would be kind. Thanks.
Upvotes: 1
Views: 35
Reputation: 8834
You don't need to replace a fragment like this
getFragmentManager().beginTransaction().replace(R.id.mainLayout,new DeliverStatsFragment()).commit();
you need to use viewpager.setCurrentItem()
for changing the position
OR
make your DeliverStatsFragment background color as white and set clickable="true"
Upvotes: 1