Tzen Szen Toh Tzen Szen Toh - 3 months ago 30
Android Question

Trouble opening new fragment

I'm having trouble with opening a new fragment when I click a button. The new fragment lies on top of the old one when I click a button on the settings. I've been through a few searches but I can't seem to fix this.

A picture and the relevant code is placed below. Any help would be appreciated.

enter image description here

Updated image (fragment not displaying properly):
enter image description here

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.phase1:
Newsfeed1Fragment newFragment = new Newsfeed1Fragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_content, newFragment);
fragmentTransaction.addToBackStack(String.valueOf(newFragment));
fragmentTransaction.commit();
break;
case R.id.phase2b:
Fragment newFragment1 = new NewsfeedFragment();
android.support.v4.app.FragmentManager fragmentManager1 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(R.id.frame_content, newFragment1);
fragmentTransaction1.addToBackStack(String.valueOf(newFragment1));
fragmentTransaction1.commit();
break;
case R.id.phase2a:
Fragment newFragment2 = new Newsfeed2aFragment();
android.support.v4.app.FragmentManager fragmentManager2 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
fragmentTransaction2.replace(R.id.frame_content, newFragment2);
fragmentTransaction2.addToBackStack(String.valueOf(newFragment2));
fragmentTransaction2.commit();
break;
case R.id.phase3a:
Fragment newFragment3 = new Newsfeed3aFragment();
android.support.v4.app.FragmentManager fragmentManager3 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();
fragmentTransaction3.replace(R.id.frame_content, newFragment3);
fragmentTransaction3.addToBackStack(String.valueOf(newFragment3));
fragmentTransaction3.commit();
break;
case R.id.phase3b:
Fragment newFragment4 = new Newsfeed3bFragment();
android.support.v4.app.FragmentManager fragmentManager4 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction4 = fragmentManager4.beginTransaction();
fragmentTransaction4.replace(R.id.frame_content, newFragment4);
fragmentTransaction4.addToBackStack(String.valueOf(newFragment4));
fragmentTransaction4.commit();
break;
case R.id.phase4:
Fragment newFragment5 = new Newsfeed4Fragment();
android.support.v4.app.FragmentManager fragmentManager5 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction5 = fragmentManager5.beginTransaction();
fragmentTransaction5.replace(R.id.frame_content, newFragment5);
fragmentTransaction5.addToBackStack(String.valueOf(newFragment5));
fragmentTransaction5.commit();
break;

default:
return super.onOptionsItemSelected(item);
}
return true;
}
}


activity_newsfeed:

<?xml version="1.0" encoding="utf-8"?>




<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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.design.widget.AppBarLayout>

<include layout="@layout/content_newsfeed" />

<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />




fragment_newsfeed:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="athena.sentineljs.com.athena.NewsfeedFragment"
tools:showIn="@layout/activity_newsfeed"
>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv" >
</android.support.v7.widget.RecyclerView>

</android.support.v4.widget.SwipeRefreshLayout>



Answer

FrameLayout by default have no background so you should define a background for that to solve your issue. So by this way previous fragment (or view behind current fragment) with not visible any more.

for second problem you mentioned in comments you can refer to this thread.

Comments