{"id":13,"date":"2012-03-08T10:05:42","date_gmt":"2012-03-08T04:35:42","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=13"},"modified":"2012-03-08T10:05:42","modified_gmt":"2012-03-08T04:35:42","slug":"android-activity-class-and-static-members","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/android-activity-class-and-static-members\/","title":{"rendered":"Android Activity class and static members"},"content":{"rendered":"<p>In one of my Android application, I was storing some data in a static member variable. After few hours of inactivity, when I used to open the application, I would see that all the data I had stored in the static member variable was gone.<\/p>\n<p>I know about the Android Activity life cycle and that Android can unload Activity class if it is not active. However I did not think that it would reset all static members too. So I had three options to persist data &#8211;<\/p>\n<ol>\n<li>Store it in preferences<\/li>\n<li>In a file (local or on external media)<\/li>\n<li>In the database<\/li>\n<li>In the Android application class<\/li>\n<\/ol>\n<p>See <a title=\"Android Data Storage Options\" href=\"http:\/\/developer.android.com\/guide\/topics\/data\/data-storage.html\" target=\"_blank\">this article<\/a>\u00a0for more details about data storage options in Android.\u00a0 Since the data is not large and I did not already have Android database created for this app \u00a0(this app does use database, but it&#8217;s HTML5 database), I decided to use option #4. Storing in application class also suited this application because I do not need the data if Android or user kills the application.<\/p>\n<p>So, be very careful when using static variables in Activity class.<\/p>\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my Android application, I was storing some data in a static member variable. After few hours of inactivity, when I used to open the application, I would see that all the data I had stored in the static member variable was gone. I know about the Android Activity life cycle and that &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/android-activity-class-and-static-members\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Android Activity class and static members&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3,1],"tags":[2],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-d","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/13"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}