Last week I was required to save one of the values for all the activities in my android app. Since I am a beginner so what I was actually looking for was :
How to save value across several Activities and all parts of your application context ?
We can certainly achieve this by creating static variable but it is not the good way as it influences towards memory leaks. The way in Android is to associate your variable with the Application context. Every Application has a context, and Android guarantees that Application context will exist as an instance across your application.
Then finally I got the solution which I wanna share here...
The way to do this is ......
create a class and extends with android.app.Application, and specify your class in the application tag in your AndroidManifest.xml file. Android will create an instance of that class and make it available for your entire application context. You can get object of your class on any activity / broadcast receiver / service in application context(environment) by Context.getApplicationContext() method.
No comments:
Post a Comment