Natural selection is testing this #Altcoins season 🌊. In this cycle, many are once again diving deep into research, searching for “the best” after Bitcoin & @Joseinnewworld makes waves 124 #NFTs — Wow, a strong signal for those still weighing their moves. #eCash $XEC #CryptoNews pic.twitter.com/GB3dRvH01U
— NFToa (@nftoa_) September 26, 2025
Hi bro, I'm confused about how to create a global variable that can maintain its value as long as the application life cycle is still running, meaning as long as the Activity is still running?
Solution
Okay, you can do it by extending the base android.app.Application class and adding some variable getters & setters as follows.
public class MyApplication extends Application {
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
}Then, declare the implementation of your class, the tag name will automatically appear in the pop up if you did it correctly when extending the Application base class.
<application
android:name=".MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name">So, you can use it in the following way, set (to throw value), get (to get value). You can do it in any Activity.
// set
((MyApplication) this.getApplication()).setSomeVariable("foo");
// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();Hope it is useful!
