1

i am trying to replace a displaynews fragment with homescreen fragment but the fragment is being adding instead of replacing in the code here

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout 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/content_main"
    android:orientation="vertical"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ajitesh.newson.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/home_fragment"
        android:name="com.ajitesh.newson.DisplayNews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

mainacitivity.class

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     fragmentClass=HomeScreen.class;

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.home_fragment,fragment).commit();

displaynews.class

 public class DisplayNews extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.displaynews, container, false);
    }
}

displaynews.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:id="@+id/display_news"
    android:layout_height="wrap_content">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="helllooooo"/>
</LinearLayout>

HomeScreen.class

public class HomeScreen extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.home_screen, container, false);
    }
}

home_screen.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:text="this is home page"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"/>

</LinearLayout>

1 Answers1

1

Fragments cannot be replaced if hard-coded in xml, you should add it dynamically to replace a fragment with another.

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

source // example -- still getting problems? You may find a solution here

Community
  • 1
  • 1
Lorenzo Lapucci
  • 129
  • 4
  • 12