Extending Application to Share Variables Globally

Extending Application to share variables globally

To elaborate on Peter K's answer, you do not need to create a second <application> section for your derived Application class. You simply need to "rename" your existing <application> section by amending it with the android:name tag. I also want to point out that the fully qualified classname is required(as you've done correctly...I found this out the hard way).

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.bpstattracker" android:versionCode="1"
android:versionName="1.0">
<application android:name="com.myname.GlobalVars" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BPStatTracker" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".BPSTAdd"></activity>
<activity android:name=".OneOrThree"></activity>
<activity android:name=".SixOrTen"></activity>

</application>
</manifest>

How to implement global variables in extends different from 'Application' and 'Activity'?

You can create a static instance of Globals and access.

public class Globals extends Application {
private static Globals instance;
private List<Float> current = new ArrayList<>();

@Override
public void onCreate() {
instance = this;
super.onCreate();
}

public float getCurrent() {
return current.get(current.size()-1);
}

public void setCurrent(float someVariable) {
this.current.add(someVariable);
}

public static Globals getInstance() {
return instance;
}

public static Context getContext(){
return instance;
// or return instance.getApplicationContext();
}
}

Now anywhere in app you can access the current variable or change the value by

Globals.getInstance().getCurrent();

Android global variable

You can extend the base android.app.Application class and add member variables like so:

public class MyApplication extends Application {

private String someVariable;

public String getSomeVariable() {
return someVariable;
}

public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
}

In your android manifest you must declare the class implementing android.app.Application (add the android:name=".MyApplication" attribute to the existing application tag):

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

Then in your activities you can get and set the variable like so:

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

Global Variable with extended Application Class

I believe the problem due to duplicate Application file is not solved completely. Remove the file from the project completely. Then go to manifest file, Application Tab. Confirm that the Name field is empty. Add the name of your application file there and click on the link. This will create the application file again. Hope this helps.



Related Topics



Leave a reply



Submit