{"id":2429,"date":"2012-11-01T11:20:07","date_gmt":"2012-11-01T11:20:07","guid":{"rendered":"http:\/\/www.mobisoftinfotech.com\/blog\/?p=2429"},"modified":"2020-08-11T19:51:51","modified_gmt":"2020-08-11T14:21:51","slug":"android-application-class-example-a-brief-tutorial-on-android-application-class","status":"publish","type":"post","link":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class","title":{"rendered":"Android Application Class Example : A Brief Tutorial On Android Application Class"},"content":{"rendered":"\n<p>Being android application developer, one may come across losing variables data which may have arisen when system kills your activity when it is in background or currently not visible. While designing android app it is very important to consider whole life cycle of application. Sometimes there are chances to miss out any of the state of the activity life cycle.<\/p>\n<p>System killed activity is one of the crucial state which can lead to unexpected behavior of app if not given attention at the time of development. Android system can kill activity of application which is currently not visible or which is in background when other visible or any priority process needs room in device memory. In such situation if developer has used some global variables which he has referred in many other activities, he can end up in losing values of those global variables when activity comes to foreground because activity where variables are instanciated has been already killed by system.<\/p>\n<p>Instead of declaring such global variables in one activity and referring in all other activities, one can create single instance class which is extended from Application class.<\/p>\n<h2><b>About Application class<\/b><\/h2>\n<p>Application class is the base class where you can maintain global variables. One can override Application class by extending his own class from it and specifying that class in AndroidManifest.xml&#8217;s tag.<br>Application class exists through out the app life cycle. Creating a static singletons can serve the purpose instead of extending and overriding Application class. But if one needs to manage other memory leaks he can override Applications class methods.<\/p>\n<p>Our own class which is extended from Application class is as follows:<\/p>\n<pre>[java]\nimport android.app.Application;\n\npublic class GlobalApplication extends Application {\n\npublic String helloFromGlobalApplication = &quot;Hello From GlobalApplication&quot;;\n\nprivate static GlobalApplication singleton;\n\npublic static GlobalApplication getInstance() {\nreturn singleton;\n}\n\n@Override\npublic void onCreate() {\nsuper.onCreate();\nsingleton = this;\n}\n}\n[\/java]<\/pre>\n<p>Then need to add this newly created class in app manifest file as follows<\/p>\n<pre>[xml]\n&amp;amp;lt;application android:name=&quot;&amp;amp;lt;your package name&amp;amp;gt;.GlobalApplication&quot;&amp;amp;gt;\n\n........\n........\n\n&amp;amp;lt;\/application&amp;amp;gt;\n[\/xml]<\/pre>\n<p>Now when you run your app, your application\/package will be created and at the same time your newly created GlobalApplication class would be instanciated. Now you can use helloFromGlobalApplication variable from GlobalApplication anywhere in other activities as follows.<\/p>\n<pre>[java]\npublic class MainActivity extends Activity {\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\nsuper.onCreate(savedInstanceState);\nsetContentView(R.layout.main);\n\nLog.d(&quot;helloFromGlobalApplication : &quot;, GlobalApplication.getInstance().helloFromGlobalApplication);\n}\n\n}\n[\/java]<\/pre>\n<p>So extending Application class helps you to create single place for global instanciation for those data or variables which you need through out the app.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Being android application developer, one may come across losing variables data which may have arisen when system kills your activity when it is in background or currently not visible. While designing android app it is very important to consider whole life cycle of application. Sometimes there are chances to miss out any of the state [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":7361,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[4],"tags":[459,299,298,300,301],"class_list":["post-2429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android","tag-android-data-lost","tag-application-class","tag-global-variables","tag-process-killed"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Android Application Class Example | Mobisoft<\/title>\n<meta name=\"description\" content=\"Android Application class is the base class where you can maintain global variables. One can override the Application class by extending his class from it and specifying that class in AndroidManifest.xml\u2019s tag. Read more.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Application Class Example | Mobisoft\" \/>\n<meta property=\"og:description\" content=\"Android Application class is the base class where you can maintain global variables. One can override the Application class by extending his class from it and specifying that class in AndroidManifest.xml\u2019s tag. Read more.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\" \/>\n<meta property=\"og:site_name\" content=\"Mobisoft Infotech\" \/>\n<meta property=\"article:published_time\" content=\"2012-11-01T11:20:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-11T14:21:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Pallavi Daga\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pallavi Daga\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#article\",\"isPartOf\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\"},\"author\":{\"name\":\"Pallavi Daga\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/6007188bd13c6f4297f1e5e2d0ac60c3\"},\"headline\":\"Android Application Class Example : A Brief Tutorial On Android Application Class\",\"datePublished\":\"2012-11-01T11:20:07+00:00\",\"dateModified\":\"2020-08-11T14:21:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\"},\"wordCount\":364,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage\"},\"thumbnailUrl\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg\",\"keywords\":[\"Android\",\"android data lost\",\"Application class\",\"global variables\",\"process killed\"],\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\",\"url\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\",\"name\":\"Android Application Class Example | Mobisoft\",\"isPartOf\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage\"},\"image\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage\"},\"thumbnailUrl\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg\",\"datePublished\":\"2012-11-01T11:20:07+00:00\",\"dateModified\":\"2020-08-11T14:21:51+00:00\",\"author\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/6007188bd13c6f4297f1e5e2d0ac60c3\"},\"description\":\"Android Application class is the base class where you can maintain global variables. One can override the Application class by extending his class from it and specifying that class in AndroidManifest.xml\u2019s tag. Read more.\",\"breadcrumb\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage\",\"url\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg\",\"contentUrl\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg\",\"width\":1920,\"height\":550,\"caption\":\"Android Application Class_l-min\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mobisoftinfotech.com\/resources\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android Application Class Example : A Brief Tutorial On Android Application Class\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#website\",\"url\":\"https:\/\/mobisoftinfotech.com\/resources\/\",\"name\":\"Mobisoft Infotech\",\"description\":\"Discover Mobility\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mobisoftinfotech.com\/resources\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/6007188bd13c6f4297f1e5e2d0ac60c3\",\"name\":\"Pallavi Daga\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/89ef33292eb327c415fedae5adc889ea2243cf389dce05bfe57552c1d8ba54fc?s=96&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/89ef33292eb327c415fedae5adc889ea2243cf389dce05bfe57552c1d8ba54fc?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/89ef33292eb327c415fedae5adc889ea2243cf389dce05bfe57552c1d8ba54fc?s=96&r=g\",\"caption\":\"Pallavi Daga\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Application Class Example | Mobisoft","description":"Android Application class is the base class where you can maintain global variables. One can override the Application class by extending his class from it and specifying that class in AndroidManifest.xml\u2019s tag. Read more.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class","og_locale":"en_US","og_type":"article","og_title":"Android Application Class Example | Mobisoft","og_description":"Android Application class is the base class where you can maintain global variables. One can override the Application class by extending his class from it and specifying that class in AndroidManifest.xml\u2019s tag. Read more.","og_url":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class","og_site_name":"Mobisoft Infotech","article_published_time":"2012-11-01T11:20:07+00:00","article_modified_time":"2020-08-11T14:21:51+00:00","og_image":[{"width":1920,"height":550,"url":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg","type":"image\/jpeg"}],"author":"Pallavi Daga","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Pallavi Daga","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#article","isPartOf":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class"},"author":{"name":"Pallavi Daga","@id":"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/6007188bd13c6f4297f1e5e2d0ac60c3"},"headline":"Android Application Class Example : A Brief Tutorial On Android Application Class","datePublished":"2012-11-01T11:20:07+00:00","dateModified":"2020-08-11T14:21:51+00:00","mainEntityOfPage":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class"},"wordCount":364,"commentCount":0,"image":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage"},"thumbnailUrl":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg","keywords":["Android","android data lost","Application class","global variables","process killed"],"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#respond"]}]},{"@type":"WebPage","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class","url":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class","name":"Android Application Class Example | Mobisoft","isPartOf":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/#website"},"primaryImageOfPage":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage"},"image":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage"},"thumbnailUrl":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg","datePublished":"2012-11-01T11:20:07+00:00","dateModified":"2020-08-11T14:21:51+00:00","author":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/6007188bd13c6f4297f1e5e2d0ac60c3"},"description":"Android Application class is the base class where you can maintain global variables. One can override the Application class by extending his class from it and specifying that class in AndroidManifest.xml\u2019s tag. Read more.","breadcrumb":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#primaryimage","url":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg","contentUrl":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2015\/01\/Android-Application-Class_l-min.jpg","width":1920,"height":550,"caption":"Android Application Class_l-min"},{"@type":"BreadcrumbList","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/android\/android-application-class-example-a-brief-tutorial-on-android-application-class#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mobisoftinfotech.com\/resources\/"},{"@type":"ListItem","position":2,"name":"Android Application Class Example : A Brief Tutorial On Android Application Class"}]},{"@type":"WebSite","@id":"https:\/\/mobisoftinfotech.com\/resources\/#website","url":"https:\/\/mobisoftinfotech.com\/resources\/","name":"Mobisoft Infotech","description":"Discover Mobility","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mobisoftinfotech.com\/resources\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/6007188bd13c6f4297f1e5e2d0ac60c3","name":"Pallavi Daga","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/89ef33292eb327c415fedae5adc889ea2243cf389dce05bfe57552c1d8ba54fc?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/89ef33292eb327c415fedae5adc889ea2243cf389dce05bfe57552c1d8ba54fc?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/89ef33292eb327c415fedae5adc889ea2243cf389dce05bfe57552c1d8ba54fc?s=96&r=g","caption":"Pallavi Daga"}}]}},"_links":{"self":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts\/2429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/comments?post=2429"}],"version-history":[{"count":40,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts\/2429\/revisions"}],"predecessor-version":[{"id":19959,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts\/2429\/revisions\/19959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/media\/7361"}],"wp:attachment":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/media?parent=2429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/categories?post=2429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/tags?post=2429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}