0

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();
}

}

Shanu
  • 169
  • 2
  • 10

1 Answers1

1

You can pass data using interfaces.

In Fragment create an interface like following.

public class YourFinalFragment extends Fragment {

  public interface onSomeEventListener {
    public void someEvent(String s);
  }

  onSomeEventListener someEventListener;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
        try {
          someEventListener = (onSomeEventListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
        }
  }
.............
button.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    someEventListener.someEvent("Test text to Fragment1");
  }
});

In your activity you have to implements that interface like below

public class MainActivity extends Activity implements onSomeEventListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       .........
    }

  @Override
  public void someEvent(String s) {
      // here you can get your desired value sent from fragment
  }
}
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
  • Thank you very much for helping me out. Did this method works when the fragment is not visible to user but has data in it? I mean when i pressed the button on final fragment all the entered data needs to be transfered to the parent activity. – Shanu Sep 21 '19 at 12:15
  • It means this is a wizard form designed by uaing fragments. So when user in the last fragment all the previous fragments are hidden. Only the last fragment is visible to user. But previous fragments has data. I mean that kind of thing – Shanu Sep 21 '19 at 12:29
  • According to you, let's say you have `fragmentA`, `fragmentB` and `fragmentFinal`. Now you have to pass data from one fragment to another like(fragmentA=>fragmentB=>fragmentFinal) and when you are in the final fragment then you can pass all data to your activity by following my answer. – Jakir Hossain Sep 21 '19 at 12:39
  • Oh i got it. Is there anyway to get data without sending from fragment A to B to final? Get all at once and send to activity from final fragment? – Shanu Sep 21 '19 at 12:47
  • yes. you can save your data to `shared preference`. and you can get it where you need it.[Note: after finish your task with this data, you have to clear your `preferences`]. – Jakir Hossain Sep 21 '19 at 12:53
  • But is it good practise for use shared prefs for this kind of task? – Shanu Sep 21 '19 at 13:32