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 !