materialsteppers icon indicating copy to clipboard operation
materialsteppers copied to clipboard

Passing data between activity and stepper fragments

Open lodogg opened this issue 8 years ago • 8 comments

Hi, I have an issue that I can't solve. In my app, I'll use the stepper to perform a configuration of an already connected bluetooth device. I need to pass the bluetooth device connectivity information from the main activity to the stepper fragments in order to send command and receive information over bluetooth.

Starting from main, my idea is to use do this:

BluetoothDevice mBtDevice

final Intent mIntent = new Intent(MainActivity.this, CalibrationActivity.class);
mIntent.putExtra("BluetoothDevice", mBtDevice);
startActivity(mIntent); 

so I will "receive" the bluetooth device in the activity that extend progressMobileStepper. At this point every thing is all right, in "mBtDevice" I have all the information I need. This is what I have done so far:

public class CalibrationActivity extends progressMobileStepper {

    List<Class> stepperFragmentList = new ArrayList<>();
    BluetoothDevice mBtDevice;

    @Override
    public List<Class> init() {

        Bundle data = getIntent().getExtras();
        mBtDevice= data.getParcelable("BluetoothDevice");


        Fragment fragment = new Fragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("toFragmentBtDevice", mBtDevice);
        fragment.setArguments(bundle);

        stepperFragmentList.add(CalibrationStep1Fragment.class);

        return stepperFragmentList;
    }

    @Override
    public void onStepperCompleted() {
        //nothing at the moment...
    }

The problem is in the "CalibrationStep1Fragment". This is the code:

public class CalibrationStep1Fragment extends stepperFragment {

   BluetoothDevice mBtDevice;

    @Override
    public boolean onNextButtonHandler() {
        return true;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle = getArguments();   // thsi is null
        mBtDevice= bundle.getParcelable("toFragmentBtDevice");

        return inflater.inflate(
                R.layout.stepper_step1, container, false);
    }

}

at this point bundle is null ( Bundle bundle = getArguments(); )

Please tell me if something is not clear and I'll try to give more information.

lodogg avatar Mar 14 '16 21:03 lodogg

till now this library is not supported for passing bundle to pass data between fragments - but surely you can save data in sharedpreference in each fragment and process/communicate between fragments.

i'll look into the way to use bundle for communication between fragments in next updats/versions.

And the above code you've tried isn't way - you must pass the class names as list and the inbuilt library does the work of creating new fragment instance and destroying it dynamically - i have implemented this because it drastically reduces the memory consumption of fragments in heap memory. - assume if you have 20+ fragment objects at once - it may leak memory/ cause overhead.

hope you can understand.

shivasurya avatar Mar 18 '16 08:03 shivasurya

This is indeed the only weakness of this library. I understand that I might work though? I mean passing bundles or not at all? Looking forward to reading you.

Isabellle avatar May 10 '16 18:05 Isabellle

@Isabellle thanks for mentioning the issue :) added bundle support and it's being tested https://github.com/shivasurya/stepperTest/commit/5e4ba5ad7a7d0750bb7f162c044c09f3503c27d7 and it'll be added in next version - i'm making it sure it doesn't make problem at any circumstance such as orientation changes,resume,pause operations.

shivasurya avatar May 11 '16 02:05 shivasurya

Sounds great, will wait until this great update is available. Thanks for all the work you've done.

Isabellle avatar May 11 '16 06:05 Isabellle

Hello, Rolling out new features and bug fix. Especially bundle to communicate between fragments have been implemented. So please stay tuned for next update

shivasurya avatar Dec 16 '16 01:12 shivasurya

Can you please update this library? i really need the addBundle method.

lecrane54 avatar Jul 10 '17 19:07 lecrane54

please can you please update the bundle

Osmandiyaka avatar Oct 31 '18 01:10 Osmandiyaka

Was this issue ever fixed? It seems it's not possible to pass data using shared preferences in the fragments as well. Kindly reach out on whether this is possible

peter-wachira avatar Jun 11 '20 13:06 peter-wachira