nikhilPK
nikhilPK

Reputation: 23

Android Fragment Duplication: Can't replace the fragment added statically

Hi i am very new in Android. I try to replace a fragment added statically in the layout file. But, if I instantiated my Fragment within my xml layout then the contents would remain visible after replacing it with another Fragment at runtime.

Here is my Activity Code:

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;

public class RetailerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retailer);
}

public void selectFrag(View view) {
    Fragment fr;

    if (view == findViewById(R.id.button2)) {
        fr = new FragmentTwo();
    } else if (view == findViewById(R.id.button3)) {
        fr = new FragmentThree();
    } else {
        fr = new FragmentOne();
    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fr);
    fragmentTransaction.commit();

 }
}

Here is my Activity XML file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:text="Fragment No.1" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:text="Fragment No.2" />

<Button
    android:id="@+id/button3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:text="Fragment No.3" />

<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<fragment
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

FragmentOne XML file:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:background="#00ffff">

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:text="This is fragment No.1"
       android:textStyle="bold" />

  </LinearLayout>

FragmentOne java file:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

    // Inflate the layout for this fragment

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

}

FragmentTwo xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" 
   android:background="#00ffff">
   <TextView
       android:id="@+id/textfrag2"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="This is fragment No.2"
       android:textStyle="bold" />

</LinearLayout>

FragmentTwo java code:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

    // Inflate the layout for this fragment

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

}

Thanks in advance.

Upvotes: 2

Views: 300

Answers (1)

Llamapuppies
Llamapuppies

Reputation: 38

I've looked into this problem too and I can't seem to find a solution for placing a fragment statically in the XML file. In your Activity XML file, you'll have to replace the Fragment portion with a FrameLayout (The id can stay the same, just replace 'Fragment' with 'FrameLayout'); only way to make your selectFrag method with FragmentTransaction replace the fragments without overlap.

(If you want to have a default fragment shown when the activity starts, duplicate the FragmentTransaction code in the onCreate method of the Activity and swap the blank FrameLayout view with either fragment)

Upvotes: 2

Related Questions