I am facing a problem and would like some insights on how I can go about solving this. Here’s a brief description
Components description:
- An
Activity
with aViewPager
, and two buttons:Add fragment
button => Adds new fragment to ViewPager with same layoutContinue
button => If user fields are validated in all fragments, goes to next screen
public class ParentActivity extends AppCompatActivity {
private InputData mfirstProfileData;
private ArrayList<InputData> mEditProfilesDataList = new ArrayList<>();
...
private EditProfilePagerAdapter mPagerAdapter;
private Button AddButton;
private Button ContinueButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
mFirstProfileData = new InputData();
...
mDotTabLayout.setupWithViewPager(mViewPager, true);
mPagerAdapter = new EditProfilePagerAdapter(getSupportFragmentManager(),
mViewPager, mDotTabLayout, mFirstProfileData);
mViewPager.setAdapter(mPagerAdapter);
mAddProfileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v ){
// TODO: validate data and save in List<InputData>
addProfile(EditProfileFragment.newInstance(getNextPageNumber(), new InputData());
}
});
mContinueBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v ){
// TODO: validate data iff one EditProfileFragment and save in List<InputData>
sendToServer();
}
});
}
...
public void addProfile(Fragment fragment) {
mPagerAdapter.addFragment(fragment);
}
public void deleteProfile () {
mPagerAdapter.deleteFragment();
}
}
- A
FragmentPagerAdapter
responsible for fragment(s) initialization, takes care of dynamic Addition/Deletion of fragments
public class EditProfilePagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<>();
private ViewPager mViewPager;
private TabLayout mDotTabLayout;
public EditProfilePagerAdapter(FragmentManager fm, ViewPager viewPager, TabLayout tabLayout, InputData firstProfileInputData) {
super(fm);
fragments.add(EditProfileFragment.newInstance(0, firstProfileInputData));
mViewPager = viewPager;
mDotTabLayout = tabLayout;
}
public void addFragment (Fragment fragment) {
fragments.add(fragment);
notifyDataSetChanged();
mDotTabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(fragments.size() - 1);
}
public void deleteFragment () {
if (mViewPager.getCurrentItem() != 0) {
fragments.remove(mViewPager.getCurrentItem());
}
notifyDataSetChanged();
mDotTabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(fragments.size() - 1);
}
...
}
- A profile
Fragment
with 5 user input fields (EditTexts
,RadioButtons
,Dialogs
) and one button:- Delete => Deletes the current fragment from the ViewPager
public class EditProfileFragment extends Fragment {
... // bunch of user input fields
private InputData mInputData;
private int mPosition;
private FloatingActionButton mDeleteBtn;
public static EditProfileFragment newInstance(int position, InputData inputData) {
EditProfileFragment fragment = new EditProfileFragment();
Bundle args = new Bundle();
args.putInt(KEY_POSITION, position);
args.putParcelable(KEY_NEW_INPUT_DATA, inputData);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPosition = getArguments().getInt(KEY_POSITION, -1);
mInputData = getArguments().getParcelable(KEY_NEW_INPUT_DATA);
}
...
// Take user input and save as mInputData.setName() etc.
...
mDeleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: send mInputData back to ParentActivity so it can be removed from the List<InputData>
((ParentActivity) getActivity()).deleteProfile();
}
});
}
...
}
Use Case:
- A user comes in the Activity with 1 fragment to start with.
- User fills out the fields in the fragment, some required ones (basic validation), he can either press
Add
in activity to add another profile fragment with same input fields. - Upon addition, User can also delete a fragment if he wants to, with the delete button is present in each fragment itself.
IMO, the triggers I have identified to pass the data along to activity are:
Add button
If the user fills the current fragment and presses the add button to get another edit profile fragment, we canvalidate
the data coming from the current fragment in the activity and save it inList<InputData>
, if validation is successful, if not, wesetError()
on the field which is missing from the required ones.Continue button
: If the user only has 1 profile (since adding another profile isn't mandatory), we can check for validation if the user presses the button before going on the next screenDelete button
: If user deletes a profile, the activity is notified so that we can remove that profile data fromList<InputData>.remove(mInputData)
I want to synchronize validation of user input and sending of user data from each fragment to the parent activity
So at the end when the user presses Continue
, I get a List<InputData>
, one for each fragment user created. Similarly, if the user presses Delete
on any fragment, the List<InputData>
gets updated to remove that fragment’s data.
How should I go about orchestrating this?
I tried implementing an interface (both in ParentActivity
and EditProfileFragment
and listening to onAddButtonPressed()
(Add button in activity) and onDeleteButtonPressed()
(Delete button in fragment) and ended up getting a circular inheritance. Any help is appreciated!