Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a View into Bundle

I'm adding dynamically created EditTexts to a layout (onClick button method). When I rotate the screen the added edittexts disappear. How can I add them to a bundle to put them in the onSavedInstanceState method? Or, is there another way to do this? I know I can save the text, but is there a way to keep the screen layout when I rotate it? If the user presses the button and adds five EditTexts(with or without typing anything) I need to save this layout when the screen is rotated (I basically need to dummy-proof my app :) ).

Thanks in advance!

like image 560
TheCommander Avatar asked Jun 25 '12 16:06

TheCommander


People also ask

How do you pass View in bundle?

You cannot add views to a bundle because they are not parcelable / serializable. The only stuff you can and should save into the bundle is the current state of the activity. You could maintain a list data structure or a counter variable that keeps track of the dynamically created views.

What does bundle do in android?

An Android App Bundle is a publishing format that includes all your app's compiled code and resources, and defers APK generation and signing to Google Play.


2 Answers

You cannot add views to a bundle because they are not parcelable / serializable. The only stuff you can and should save into the bundle is the current state of the activity.

You could maintain a list data structure or a counter variable that keeps track of the dynamically created views. Also save the string values of the EditTexts. In onCreate you should interpret that information in order to recreate the views and their states.

like image 62
tiguchi Avatar answered Oct 07 '22 15:10

tiguchi


you can't save a view. bundle is just a pair name-value. of course you can save the text and then render it again. the way is just you said.

onsaveInsatnceState(bundle save) save the text as save.putString('text1','bla bla') of course you need a loop to save all your edittext.

then in oncreated( bundle save)

you can get what you have added by save.getString('text1')

like image 32
Xenione Avatar answered Oct 07 '22 15:10

Xenione