Я выполняю действие с табулатом (используя ViewPager и FragmentPagerAdapter). Каждая из вкладок представляет собой отдельные фрагменты. На одной из вкладок (CheckInNewRecipient) есть два фрагмента: один внизу для отображения списка контактов для выбора и один сверху для контактов, которые были выбраны.
Возникает недопустимое исключение состояния, при котором фрагмент вкладки CheckInNewRecipient добавляется дважды. Этот фрагмент создается (onAttach и onCreate) один раз при запуске табуляции. Когда я начинаю заполнять подфрагменты внутри него, он добавляет два подфрагмента и затем пытается снова загрузить основной фрагмент. В моем коде нет ничего, что говорило бы о перезагрузке этого фрагмента, кроме заполнения обоих субфрагментов.
Прикреплено недопустимое исключение состояния из logcat
2020-04-07 07:49:23.819 11883-11883/com.grgapps.checkingin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.grgapps.checkingin, PID: 11883
java.lang.IllegalStateException: Fragment already added: CheckInNewRecipients{722367} (1cda137a-bd2a-40b3-b65d-e4e3ddd4eafd) id=0x7f09006d android:switcher:2131296365:2}
at androidx.fragment.app.FragmentManager.addFragment(FragmentManager.java:1628)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:405)
at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2333)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2120)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2075)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1977)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:417)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Ниже приведен onCreateView во фрагменте, являющемся одной из вкладок.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "CheckInNewRecipients onCreateView: ");
context = getActivity();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
ContactsSelectionListDisplay contactsSelectionListDisplay = new
ContactsSelectionListDisplay(R.layout.activity_check_in_new, getActivity());
Log.i(TAG, "CheckInNewRecipients onCreateView: initializing ContactsSelectionAndDisplay ");
ContactsFilterAndDisplay contactsFilterAndDisplay =
new ContactsFilterAndDisplay(R.layout.activity_check_in_new, getActivity());
Log.i(TAG, "CheckInNewRecipients onCreateView: initializing ContactsFilterAndDisplay ");
transaction.add(R.id.check_in_recipient_selection_list_recycler_view,contactsSelectionListDisplay).commit();
Log.i(TAG, "CheckInNewRecipients onCreateView: after add of ContactsSelectionListDisplay" + this.context);
transaction.add(R.id.check_in_recipient_contacts_list_recycler_view, contactsFilterAndDisplay).commit();
Log.i(TAG, "CheckInNewRecipients onCreateView: after transaction.add statements");
return inflater.inflate(R.layout.fragment_check_in_new_recipients, container, false);
}
Это макет фрагмента для CheckInNewRecipients
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".CheckInNewRecipients">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/fragment_contacts_selection_list_display" />
<include layout="@layout/fragment_contacts_filter_and_display" />
</LinearLayout>
</FrameLayout>
Ниже приведено первое включение из приведенного выше макета фрагмента.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
tools:context=".ContactsSelectionListDisplay">
<!-- TODO: Update blank fragment layout -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/check_in_recipient_selection_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:divider="#467FD7"
android:dividerHeight="10dp"
tools:listitem="@layout/recycler_view_check_in_recipient_selected_list"/>
</FrameLayout>
Ниже приводится второе включение из макета фрагмента
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="350dp"
tools:context=".ContactsFilterAndDisplay">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="108dp">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="55dp"
android:orientation="horizontal">
</RadioGroup>
<RadioButton
android:id="@+id/radio_all_contacts"
android:layout_width="80dp"
android:layout_height="55dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="328dp"
android:buttonTint="#2196F3"
android:drawableTint="#2196F3"
android:onClick="onRadioButtonClicked"
android:text="@string/radio_contact_filter_all"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_app_users"
android:layout_width="100dp"
android:layout_height="55dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="228dp"
android:buttonTint="#2196F3"
android:drawableTint="#2196F3"
android:onClick="onRadioButtonClickedAppUsers"
android:text="@string/radio_contact_filter_app_users"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_by_group"
android:layout_width="100dp"
android:layout_height="55dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="120dp"
android:buttonTint="#2196F3"
android:drawableTint="#2196F3"
android:onClick="onRadioButtonClickedByGroup"
android:text="@string/radio_contact_filter_by_group"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_filter_search"
android:layout_width="100dp"
android:layout_height="55dp"
android:layout_marginStart="304dp"
android:layout_marginTop="44dp"
android:buttonTint="#2196F3"
android:drawableTint="#2196F3"
android:onClick="onRadioButtonClickedSearch"
android:text="@string/radio_contact_filter_search"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView14"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/title_contacts_filter"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/check_in_recipient_contacts_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:divider="#467FD7"
android:dividerHeight="10dp"
tools:listitem="@layout/recycler_view_check_in_contacts_list" />
</LinearLayout>
</FrameLayout>
Ниже приведен полный код CheckInNewRecipients
package com.grgapps.checkingin;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import javax.security.auth.login.LoginException;
import static android.view.View.getDefaultSize;
import static androidx.constraintlayout.widget.Constraints.TAG;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link CheckInNewRecipients.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link CheckInNewRecipients#newInstance} factory method to
* create an instance of this fragment.
*/
public class CheckInNewRecipients extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private Context context;
private OnFragmentInteractionListener mListener;
public CheckInNewRecipients() {
// Required empty public constructor
Log.i(TAG, "CheckInNewRecipients: ");
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment CheckInNewRecipients.
*/
// TODO: Rename and change types and number of parameters
public static CheckInNewRecipients newInstance(String param1, String param2) {
CheckInNewRecipients fragment = new CheckInNewRecipients();
Log.i(TAG, "CheckInNewRecipients newInstance: ");
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "CheckInNewRecipients onCreate: ");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
Log.i(TAG, "CheckInNewRecipients onCreate: On Create Completed");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "CheckInNewRecipients onCreateView: ");
context = getActivity();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
ContactsSelectionListDisplay contactsSelectionListDisplay = new
ContactsSelectionListDisplay(R.layout.activity_check_in_new, getActivity());
Log.i(TAG, "CheckInNewRecipients onCreateView: initializing ContactsSelectionAndDisplay ");
ContactsFilterAndDisplay contactsFilterAndDisplay =
new ContactsFilterAndDisplay(R.layout.activity_check_in_new, getActivity());
Log.i(TAG, "CheckInNewRecipients onCreateView: initializing ContactsFilterAndDisplay ");
transaction.add(R.id.check_in_recipient_selection_list_recycler_view,contactsSelectionListDisplay).commit();
Log.i(TAG, "CheckInNewRecipients onCreateView: after add of ContactsSelectionListDisplay" + this.context);
transaction.add(R.id.check_in_recipient_contacts_list_recycler_view, contactsFilterAndDisplay).commit();
Log.i(TAG, "CheckInNewRecipients onCreateView: after transaction.add statements");
return inflater.inflate(R.layout.fragment_check_in_new_recipients, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
Log.i(TAG, "CheckInNewRecipients onButtonPressed: ");
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "CheckInNewRecipients onAttach: ");
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
Log.i(TAG, "CheckInNewRecipients onAttach: On Attach Completed");
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
// FragmentManager transaction = getActivity().getSupportFragmentManager();
//transaction.beginTransaction().add(R.id.check_in_recipient_selection_list_recycler_view,
// contactsSelectionListDisplay).commit();
//transaction.beginTransaction().add(R.id.check_in_recipient_contacts_list_recycler_view,
// contactsFilterAndDisplay).commit();
Ниже приведен код java одного из фрагментов. Второй фрагмент отражает первый.
package com.grgapps.checkingin;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar;
import java.util.List;
import java.util.Objects;
import static androidx.constraintlayout.widget.Constraints.TAG;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ContactsSelectionListDisplay.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ContactsSelectionListDisplay#newInstance} factory method to
* create an instance of this fragment.
*/
public class ContactsSelectionListDisplay extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private RecyclerView recyclerView;
private RecyclerView.Adapter checkInContactSelectionListAdapter;
//private RecyclerView.LayoutManager layoutManager;
private MainViewModel mViewModel;
private CheckInContactsSelectionListDisplayAdapter mAdapter;
private MainViewModelProviderFactory viewModelFactory;
private TextView checkInContactsPhotoThumbnail;
private TextView checkInContactsDisplayLastName;
private TextView checkInContactsDisplayFirstName;
private TextView checkInContactsListGroups;
private Context context;
private ContactsSelectionListDisplay.OnFragmentInteractionListener mListener;
public ContactsSelectionListDisplay(int layoutID, Context context) {
// Required empty public constructor
Log.i(TAG, "ContactsSelectionListDisplay: ");
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
//* @param param1 Parameter 1.
//* @param param2 Parameter 2.
* @return A new instance of fragment ContactsSelectionLIstDisplay.
*/
// TODO: Rename and change types and number of parameters
public static ContactsSelectionListDisplay newInstance(int layoutid, Context context) {
ContactsSelectionListDisplay fragment =
new ContactsSelectionListDisplay(R.layout.activity_check_in_new, context);
Bundle args = new Bundle();
//args.putString(ARG_PARAM1, param1);
//args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
Log.i(TAG, "ContactsSelectionListDisplay newInstance: ");
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "ContactsSelectionListDisplay onCreate: ");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.i(TAG, "ContactsSelectionListDisplay onCreateView: "+ context);
viewModelFactory = new MainViewModelProviderFactory(this.context);
mViewModel = new ViewModelProvider(this, viewModelFactory).get(MainViewModel.class);
mAdapter = new CheckInContactsSelectionListDisplayAdapter(R.layout.fragment_contacts_selection_list_display, context);
RecyclerView recyclerView = getActivity().findViewById(R.id.check_in_recipient_selection_list_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(mAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
LinearLayoutManager.VERTICAL));
return inflater.inflate(R.layout.fragment_contacts_selection_list_display, container, false);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "ContactsSelectionListDisplay : OnAttach");
viewModelFactory = new MainViewModelProviderFactory(context.getApplicationContext());
mViewModel = new ViewModelProvider(this, viewModelFactory).get(MainViewModel.class);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "ContactsSelectionListDisplay: On Activity Created");
context = this.getContext();
mViewModel = new ViewModelProvider(this, viewModelFactory).get(MainViewModel.class);
mAdapter = new CheckInContactsSelectionListDisplayAdapter(R.layout.fragment_contacts_selection_list_display, context);
RecyclerView recyclerView = getView().findViewById(R.id.check_in_recipient_selection_list_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(mAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
LinearLayoutManager.VERTICAL));
observerSetup();
Log.i(TAG, "ContactsSelectionListDisplay onActivityCreated: Observer SetUp Complete");
ItemTouchHelper.SimpleCallback callback = new
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
mAdapter.deleteItem(position);
Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item " + (direction == ItemTouchHelper.RIGHT ? "deleted" : "archived") + ".", Snackbar.LENGTH_LONG);
snackbar.setDuration(3000);
snackbar.setAction(android.R.string.cancel, new View.OnClickListener() {
@Override
public void onClick(View view) {
mAdapter.undoDelete();
}
});
snackbar.show();
}
@Override
public void onChildDraw (Canvas c, RecyclerView
recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive){
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
.addSwipeLeftBackgroundColor(ContextCompat.getColor(getContext(), R.color.recycler_view_item_swipe_left_background))
.addSwipeLeftActionIcon(R.drawable.ic_archive_white_24dp)
.addSwipeRightBackgroundColor(ContextCompat.getColor(getContext(), R.color.recycler_view_item_swipe_right_background))
.addSwipeRightActionIcon(R.drawable.ic_delete_white_24dp)
.addSwipeRightLabel(getString(R.string.action_delete))
.setSwipeRightLabelColor(Color.WHITE)
.addSwipeLeftLabel(getString(R.string.action_archive))
.setSwipeLeftLabelColor(Color.WHITE)
.create()
.decorate();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(recyclerView);
CustomToast newtoast = new CustomToast(getContext(),"you clicked on this" );
newtoast.show();
Log.i(TAG, "ContactsSelectionListDisplay onActivityCreated: Completed OnActivityCreated");
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
private void observerSetup() {
Log.i(TAG, "ContactsSelectionListDisplay observerSetup:");
checkInContactsPhotoThumbnail = getView().findViewById(R.id.checkInRecipientSelectedPhotoThumbnail);
checkInContactsDisplayLastName = getView().findViewById(R.id.checkInRecipientSelectedLastName);
checkInContactsDisplayFirstName = getView().findViewById(R.id.checkInRecipientSelectedFirstName);
checkInContactsListGroups = getView().findViewById(R.id.checkInRecipientSelectedGroups);
if(mViewModel.getSelectionListContacts() != null)
Log.i(TAG, "ContactsSelectionListDisplay getSelectionListContacts != null observerSetup: ");
Objects.requireNonNull(mViewModel.getSelectionListContacts()).observe(getViewLifecycleOwner(),
new Observer<List<ContactTable>>() {
@Override
public void onChanged(@Nullable final List<ContactTable> allSelectionListContacts) {
Log.i(TAG, "ContactsSelectionListDisplay observer setup onChanged: ");
mAdapter.setContactSelectionList(allSelectionListContacts);
}
});
}
}