0

I'm having trouble replacing my fragments. I have action bar at the top, and my fragment is covered by frame layout. Here's the xml layout.

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FrontAccidentCamActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/front_actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/front_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id = "@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/front_actionbar"
    >

    <fragment
        android:id ="@+id/front_frags"
        android:name="acc_fragments.FrontClose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</FrameLayout>

And here's my Activity and fragments.

public class FrontAccidentCamActivity extends AppCompatActivity {


private Toolbar toolbar;

// Fragments
private FragmentManager manager;
private FrontClose frontClose;
private FrontFar frontFar;



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

    toolbar = findViewById(R.id.front_toolbar);
    toolbar.setTitle("전방 사고 현장 도우미");

    setSupportActionBar(toolbar);

    // fragments casting
    manager = getSupportFragmentManager();

    frontClose= (FrontClose) manager.findFragmentById(R.id.front_frags); // index = 0

    frontFar = new FrontFar();              // index : 1



}

public void toNextFragment(int index) {
    if (index == 1) {
        manager.beginTransaction().replace(R.id.frameLayout, frontFar).commit();

    }
}

This is the fragment!

public class FrontClose extends Fragment  {

// my index = 0
Button btnNext0;
FrontAccidentCamActivity activity ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.frag_front_close, container, false);

    (btnNext0 = view.findViewById(R.id.btnNext0)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity = (FrontAccidentCamActivity)getActivity();

            activity.toNextFragment(1);
        }
    });


    return view;
}

I tried replacing it without the frame layout, I tried a lot of things but the duplication problem is not solved. Any help would be appreciated. I searched for a long time and debugged it a lot. It was a complicated code but I narrowed it down as above. Please help !

Peter
  • 37
  • 1
  • 7
  • 1
    You cannot remove/replace `Fragment`s declared statically in your layout. You would need to remove that `` element, and load `FrontClose` into `frameLayout` yourself in code, at startup. Your subsequent `replace()` transactions should then work as expected. – Mike M. Dec 13 '18 at 05:11
  • how can I add fragment into frameLayout by code? – Peter Dec 13 '18 at 13:11
  • I deleted the fragment xml and added manager.beginTransaction().replace(R.id.frameLayout, frontClose); to my onCreate but it doesn't work :( :( :( – Peter Dec 13 '18 at 13:16
  • 1
    Oh I didn't commit it... :) Never mind it worked!! Thank you soooooooooooooooooo much ^^ – Peter Dec 13 '18 at 14:08

1 Answers1

0

you should try fragment add instead of replace

public void toNextFragment(int index) {
    if (index == 1) {
        manager.beginTransaction().add(R.id.frameLayout, frontFar).commit();

    }
}

for more details refer to this link

Rahul Chokshi
  • 670
  • 4
  • 18
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33