Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application Level Scope Variables

I understand that there is no Application Level Scope available to be able to define shared logic/data, and that each activity is essentially a stand alone application tied together by the manifest...

BUT I have a group of user access permissions that I am getting via a web service call and do not want to make this call in every activities onCreate().

I am using SharedPreferences in the application, but since the permissions are not intended to be editable by the user, the flat file exposure on android devices feels like an insecure way to handle this.

I do need to re-request this info if the application is restarted, so I believe the least expensive would be to store it in a variable.

I am aware of IntentExtras but we are talking about a Settings "Object", not a primitive type.

right way to handle this situation?

like image 727
jondavidjohn Avatar asked Jun 30 '11 19:06

jondavidjohn


1 Answers

You can actually create an "Application" class that can be used to essentially create Application wide settings.

Just create a new class and extend Application, then set any class members and appropriate getter/setter methods and you can access them throughout your application.

You then need to update your manifest as follows:

<application android:icon="@drawable/logo"
             android:label="@string/app_name"
             android:name=".application.CustomApplication">

Then in any activity you can access it as follows:

CustomApplication app = ((CustomApplication)getApplication());
like image 179
rhinds Avatar answered Oct 20 '22 17:10

rhinds