While working on an app, I had an activity that downloaded an image from the Internet and displayed it to the user (amongst other things). Because all of the data presented to the user is downloaded from the Internet, it's mandatory for me to save this data in onSaveInstanceState()
and restore it in onCreate()
.
At first, I had a problem. I was downloading the image to a Drawable
, using Drawable.createFromStream()
. Now, because a Drawable
is dependent on its Context
, it can't be serialized or parceled.
The solution was to use a Bitmap
instead, which thankfully implements Parcelable
. Downloading a Bitmap
is easy:
URL url = new URL("http://www.example.com/pic.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
Then saving it in the instance state is even easier:
public void onSaveInstanceState(Bundle outState) {
outState.putParcelable("bitmap", bitmap);
}
Restoring:
if (savedInstanceState != null) bitmap = (Bitmap) savedInstanceState.getParcelable("bitmap");
And there you have it, simple as pie.
This was exactly what I was looking for. Thanks.
ReplyDeleteStraight to the point. Thanks.
ReplyDeleteThank you! Was wondering how to store a bitmap in onSaveInstanceState :)
ReplyDeleteNice post. It is really interesting. Thanks for sharing the post!
ReplyDeleteDigital Marketing Services in India
Your Affiliate Money Printing Machine is ready -
ReplyDeletePlus, making money online using it is as easy as 1-2-3!
This is how it works...
STEP 1. Tell the system what affiliate products the system will promote
STEP 2. Add PUSH button traffic (this LITERALLY takes 2 minutes)
STEP 3. Watch the affiliate products system grow your list and sell your affiliate products on it's own!
Are you ready???
Click here to launch the system
Excellent post! Your post is very useful and I felt quite interesting reading it. Expecting more post like this. Thanks for posting such a good post. laptop service in home. To service your laptop with offer prices, Please visit : Laptop service center in Navalur
ReplyDeleteHVAC & Plumbing Services
ReplyDeleteAir Star Heating guarantees reliability and quality for all equipment and services.
Air Star Heating specialists always try to deliver the most excellent quality of services to our customers at an affordable price. It is understood that every client has different needs and different problems. We try to accomplish the needs of every client according to their requests. We are having considerable experience in this field. Our specialists understand very well how things work. It doesn’t matter in which field of industry you are looking for services.
Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
Visit:- https://airstarheating.com
Excellent post! Your post is very useful and I felt quite interesting reading it.
ReplyDeleteslotmandiri888.com
slotmandiri888.com
slotmandiri888.com
hihiihi
ReplyDeleteHi nice reading your poost
ReplyDelete