android


How to save custom Objects onSaveInstanceState without implementing parcelable


i need to save a custom object that i use in a fragment so it will not be lost when the screen rotates (when the app calls onDestroy and then recalls onCreate)
now the normal way to do so is to implement Parcelable interface and save it to the bundle as a Parcelable object.
that is a very tedious way of doing things.
is there a way to just pass the object along as "putObject" method?
You can save your data in fragment, retained during a configuration change like in example.
Extend the Fragment class and declare references to your stateful
objects.
public class RetainedFragment extends Fragment {
// data object we want to retain
private MyDataObject data;
// this method is only called once for this fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain this fragment
setRetainInstance(true);
}
.. getter and setter
}
Then use FragmentManager to add the fragment to the activity.
public class MyActivity extends Activity {
private RetainedFragment dataFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
..
// find the retained fragment on activity restarts
FragmentManager fm = getFragmentManager();
dataFragment = (RetainedFragment) fm.findFragmentByTag(“data”);
// create the fragment and data the first time
if (dataFragment == null) {
// add the fragment
dataFragment = new DataFragment();
fm.beginTransaction().add(dataFragment, “data”).commit();
} else {
// available dataFragment.getData()
..
// save data in onDestroy dataFragment.setData(yourData);
The best way is to implement Parcelable (Faster).
Easier (not efficient) way is to implement Serializable and add the object into the bundle as serializable.
well searching i found no official way of doing so, so here are two "hacks" i found around the problem:
1)create a class that extends Application class, in it add an arrayList of objects.
inside onSaveInstanceState call:
getApplication().getObjectArray().add(YourObject);
save the Object index inside the bundle using putInt.
extract it inside the method onReturnestoreInstanceState.
2)my less favorite one:
android automatically saves the states of its views
therefor a way to save an object will be to create a view set its visibility to none so it wont show on the screen and then add each object we want to the view using the methods:
view.setTag(key,Object); or view.setTag(Object);
now inside onReturnestoreInstanceState get the view and extract the tags.
unfortunately i couldn't find a more simple way of saving an object
hope this one helps you out (in my app i ended up using the first method)

Related Links

Is there any way to connect printer from Android phones through wifi for printing?
Select an icon in a button android
How to stop one body when collide with another body [Box2d]
showDatePickerDialog can't load DatePicker Dialog
Permission issues while trying to compile native code
sherlock SearchView always collapsed
How to pass data between activities
Add a LinearLayout in bundle
Android start system service at app startup
add animation to listview items
Android Disable airplane mode at version >=4.1
Android: Adding many children to a layout in a separate thread
Robotium with parameter
Android navigating to parent Fragment Activity from within Fragments
How to query rows in between two datetime columns?
App crashes when receiving of message

Categories

HOME
sonarqube
turing-machines
gdb
string
twig
devise
javacc
entity-framework-core
web-hosting
jetty
flume
extjs4.2
p2p
jquery-ui-sortable
fhir
mxgraph
speech-synthesis
symmetricds
flatpak
docx
job-scheduling
microsoft-dynamics-nav
google-data-studio
xmonad
tosca
ms-access-web-app
genexus-sd
angular-translate
tpm
bringtofront
mql
vex
sqsh
jmockit
hard-drive
openwhisk
carriage-return
jpad
cin
visa
dcmtk
knime
optimizely
posixct
oracleclient
colorbar
mouseleave
d3-force-directed
baidu
juice-ui
reporting-services-2012
google-cloud-console
dotnetnuke-7
ellucian-scribe
httpruntime.cache
inf
fuelcms
phonertc
mirror
keyup
omnifaces
nsurlsessiondatatask
matlab-deployment
fail2ban
user-permissions
rapidsvn
viewer
sip-server
entry-point
tweenlite
ignite-ui
matrix-inverse
winginx
extjs2
accesscontrolexception
light
model-driven-development
video-codecs
superscrollorama
chart-director
pstack
xcelsius
update-statement
pylucene
http-daemon
tmx
luajava
metaweblog
ie-compatibility-mode
aspmenu
folding
glui
great-circle

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App