My application has one Main Activity and 13 Fragments. There is a FragmentAdapter to Change Fragments on Next or Previous Button click.All 13 fragments had different components that user interacting. At the final fragment, there is a button and by pressing that button all the entered data should be passed to the parent activity(MainActvity). After passing data do some work in the activity. So i want to know that the best way to achieve this requirement. This is my Main Activity that sets the fragment adapter.
public class HomeActivity extends AppCompatActivity {
private StepperLayout mStepperLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mStepperLayout = findViewById(R.id.stepperLayout);
mStepperLayout.setAdapter(new MyStepperAdapter(getSupportFragmentManager(), this));
}
}
This is the FragmentAdapter.
public class MyStepperAdapter extends AbstractFragmentStepAdapter {
public MyStepperAdapter(FragmentManager fm, Context context) {
super(fm, context);
}
@Override
public Step createStep(int position) {
switch (position) {
case 0:
final VehicleInformation step1 = new VehicleInformation();
return step1;
case 1:
final DriverHelperDetails step2 = new DriverHelperDetails();
return step2;
case 2:
final LegalDocument step3 = new LegalDocument();
return step3;
case 3:
final LightCondition step4 = new LightCondition();
return step4;
case 4:
final TyreCondition step5 = new TyreCondition();
return step5;
case 5:
final VehicleCabinCondition step6 = new VehicleCabinCondition();
return step6;
case 6:
final OtherConditions step7 = new OtherConditions();
return step7;
case 7:
final TankerCondition step8 = new TankerCondition();
return step8;
case 8:
final CompressorCondition step9 = new CompressorCondition();
return step9;
case 9:
final OtherEquipCondition step10 = new OtherEquipCondition();
return step10;
case 10:
final EmergencyTEquip step11 = new EmergencyTEquip();
return step11;
case 11:
final PersonalProtectEquip step12 = new PersonalProtectEquip();
return step12;
case 12:
final Complete step13 = new Complete();
return step13;
}
return null;
}
This is one example Fragment.
public class VehicleInformation extends Fragment implements BlockingStep {
Button btn_next;
TextInputEditText vname;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.vehicleinformation, container, false);
vname = rootView.findViewById(R.id.v_no);
return rootView;
}
@Nullable
@Override
public VerificationError verifyStep() {
return null;
}
@Override
public void onSelected() {
}
@Override
public void onError(@NonNull VerificationError error) {
}
@Override
@UiThread
public void onNextClicked(StepperLayout.OnNextClickedCallback callback) {
callback.goToNextStep();
}
@Override
@UiThread
public void onCompleteClicked(final StepperLayout.OnCompleteClickedCallback callback) {
}
@Override
@UiThread
public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {
callback.goToPrevStep();
}
}