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
Usb Device Access Pop-Up Suppression
Get the Current Fragment Object
Use a Custom Contextual Action Bar for Webview Text Selection
Using Viewpagerindicator Library with Android Studio and Gradle
Using Android Vector Drawables on Pre Lollipop Crash
Get Uri of .Mp3 File Stored in Res/Raw Folder in Android
Show Timepicker with Minutes Intervals in Android
Mediacodec and Camera: Colorspaces Don't Match
Method Not Found Using Digestutils in Android
Google Cloud Endpoints and User's Authentication
Why Is Android Studio Reporting "Uri Is Not Registered"
Using Global Exception Handling on Android
Google Signin Not Working in Release Mode APK Android
How to Find and Turn on Usb Debugging Mode on Nexus 4
Android Dependency Has Different Version for the Compile and Runtime
Adb Root Is Not Working on Emulator (Cannot Run as Root in Production Builds)