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

My App shows popup recommending some App for which I did not write any code
How to align six imagebuttons in 2 columns with 3 rows in android studio?
Google Map Implementation in Viewpager, Error Unable to get provider com.google.firebase.provider.FirebaseInitProvider
set Hindi as TextToSpeech language in android programatically
How to make a link redirecting on Apple store or Google play
How to rotate image slowly in android?
Find element by class-name and content-desc in web context selenium
Retrofit - Failure in SSL library, usually a protocol error
Redirect site page on Intel XDK
How to implement own cast.receiver.media.Player
Titanium View.toImage() and base64 encode not always decoding properly
How does Firebase crash reporting initialize
AVD errors with HAX on Android Studio 2.1
Slider and ngModel lagging with finger slide
How to set proper version control for Ionic project
Android Studio Gradle task stuck

Categories

HOME
sonarqube
ggplot2
amp-html
components
yandex
big-o
interpolation
spring-session
jax-ws
extjs4.2
32bit-64bit
jquery-ui-sortable
appcelerator-studio
text-mining
mod-security
project-intu
cmdb
enterprise-architect
libusb
glyphicons
dimensional-modeling
feathersjs
asp.net-mvc-viewmodel
poedit
ksh
r-mice
pchart
tosca
highstock
comparator
beta
xunit
motion-detection
entity-relationship-model
linked-data
android-things
alm
scrollview
gitlab-api
complex-numbers
left-join
dumpbin
papaparse
jpad
xdocreport
password-protection
metaprogramming
parser-generator
install.packages
ssrs-2014
opennms
lampp
openrasta
chai-as-promised
mschart
lightning
quicktime
type-theory
swingworker
rxtx
rsa-archer-grc
polyline
smartfoxserver
stripe-connect
chromium-os
redistributable
seek
ng-grid
cifilter
shopping
embedding
qiime
project-organization
nullable
qbxml
contentcontrol
sciruby
dllimport
include-guards
slash
viola-jones
branch-and-bound
sql-server-data-tools
ansi
typeconverter
rapidsvn
device-tree
raygun
build-script
memset
mutual-authentication
android-framework
dsym
getopt
android-usb
mt4j
artemis
embedded-fonts
windows-phone-8-sdk
model-driven-development
insert-id
mantle
apache-abdera
idn
frameworkelementfactory
zope.interface
faye
garbage
jdb
virtualpathprovider
procedures
entity-attribute-value
workflow-services
update-statement
mysql-error-1205
webdev.webserver
lr
burndowncharts
pagemethods
skype4java
lobo-cobra
online-storage
mathematical-typesetting
wspbuilder

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