简体   繁体   中英

How to open a fragment from different fragment with some spinner's value set?

I have two fragments. One fragment contains a pie-chart and some buttons. On clicking the pie-chart or buttons, a second fragment is to be opened which has some spinners. Now, the contents of the second fragment are rendered by those spinners. Values of these spinners are depended on the buttons clicked. How can I send this information to those spinners? The information which is to be transferred is in form of objects.

  1. Use Bundles for passing arguments for your Fragment. Never use setters/getters, because those could not correspond with your fragment's lifecycle. Create public constants in the new fragment in order to use them as keys in your Bundle hashmap.

  2. Depending on your layouts hierarchy, use getActivity().getSupportFragmentManager() (in case you want the new fragment to be opened on the same 'level' of hierarchy as the previous one) or getChildFragmentManager() (if you want the new fragment to be inside of the previous one).

Hope this helps!

If you want to pass a model object bewteen two activities/fragments create your POJO as per usual. You can quickly make this class parcelable by going to Android Studio > Preferences > Plugins > search parcelable and install Android Parcelable code generator. Restart Android studio. Then Code > Generate > Parcelable will implement all the necessary methods on your class. You should be able to do something like:

Bunlde bundle = new Bundle();
bundle.putParcelable("key", YourPojoWhichHasImplimentedParcelable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM