How can I create global variable keep remain values around the life cycle of the application regardless which activity running.
import android.app.Application;
public class Globals extends Application
{
private static Globals instance = null;
private static int RecentCompaignID;
private static int EmailClick;
private static String LoginPassword;
static String loginMemberID;
private static String CompaignName = "";
private static int listget=0;
//MailingDetails
private static String FromEmailadd="";
private static String FromName="";
private static String ReplyEmailAdd="";
private static String CompaignSubject="";
private static int TempId=0;
private static int ListIds=0;
private static String HTMLContent="";
@Override
public void onCreate()
{
super.onCreate();
instance = this;
}
public static Globals getInstance()
{
return instance;
}
public void setRecentCompaignID(int objRecentCompaignID)
{
RecentCompaignID = objRecentCompaignID;
}
public int getRecentCompaignID()
{
return RecentCompaignID;
}
public void setLoginMemberID(String objloginMemberID)
{
loginMemberID = objloginMemberID;
}
public String getLoginMemberID()
{
return loginMemberID;
}
public void setLoginMemberPassword(String objLoginPassword)
{
LoginPassword = objLoginPassword;
}
public String getLoginMemberPassword()
{
return LoginPassword;
}
public void setEmailclick(int id)
{
EmailClick = id;
}
public int getEmailClick()
{
return EmailClick;
}
public void setCompaignName(String objCompaignName)
{
CompaignName=objCompaignName;
}
public String getCompaignName()
{
return CompaignName;
}
public void setlistgetvalue(int objlistget)
{
listget=objlistget;
}
public int getlistvalue()
{
return listget;
}
public void setCompaignSubject(String objCompaignSubject)
{
CompaignSubject=objCompaignSubject;
}
public String getCompaignSubject()
{
return CompaignSubject;
}
public void setHTMLContent(String objHTMLContent)
{
HTMLContent=objHTMLContent;
}
public String getHTMLContent()
{
return HTMLContent;
}
public void setListIds(int objListIds)
{
ListIds=objListIds;
}
public int getListIds()
{
return ListIds;
}
public void setReplyEmailAdd(String objReplyEmailAdd)
{
ReplyEmailAdd=objReplyEmailAdd;
}
public String getReplyEmailAdd()
{
return ReplyEmailAdd;
}
public void setFromName(String objFromName)
{
FromName=objFromName;
}
public String getFromName()
{
return FromName;
}
public void setFromEmailadd(String objFromEmailadd)
{
FromEmailadd=objFromEmailadd;
}
public String getFromEmailadd()
{
return FromEmailadd;
}
}
Try Like This:
Create a shared data class:
SharedData.java
import android.app.Application;
/**
* Created by kundan on 6/23/2015.
*/
public class Globals {
private static Globals instance = new Globals();
// Getter-Setters
public static Globals getInstance() {
return instance;
}
public static void setInstance(Globals instance) {
Globals.instance = instance;
}
private String notification_index;
private Globals() {
}
public String getValue() {
return notification_index;
}
public void setValue(String notification_index) {
this.notification_index = notification_index;
}
}
Declared/Initiaze an instance of class globally in those classes where you want to set/get data (using this code before onCreate()
method):-
Globals sharedData = Globals.getInstance();
Set data:
sharedData.setValue("kundan");
Get data:
String n = sharedData.getValue();
You could use application preferences. They are accessible from any activity or piece of code as long as you pass on the Context object, and they are private to the application that uses them, so you don't need to worry about exposing application specific values, unless you deal with routed devices. Even so, you could use hashing or encryption schemes to save the values. Also, these preferences are stored from an application run to the next. Here is some code examples that you can look at.
Technically this does not answer the question, but I would recommend using the Room database instead of any global variable. https://developer.android.com/topic/libraries/architecture/room.html Even if you are 'only' needing to store a global variable and it's no big deal and what not, but using the Room database is the most elegant, native and well supported way of keeping values around the life cycle of the activity. It will help to prevent many issues, especially integrity of data. I understand that database and global variable are different but please use Room for the sake of code maintenance, app stability and data integrity.
You can create a Global Class
like this:
public class GlobalClass extends Application{
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public String getEmail() {
return email;
}
public void setEmail(String aEmail) {
email = aEmail;
}
}
Then define it in the manifest:
<application
android:name="com.example.globalvariable.GlobalClass" ....
Now you can set values to global variable like this:
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
globalVariable.setName("Android Example context variable");
You can get those values like this:
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
final String name = globalVariable.getName();
Please find complete example from this blog Global Variables
// My Class Global Variables Save File Global.Java
public class Global {
public static int myVi;
public static String myVs;
}
// How to used on many Activity
Global.myVi = 12;
Global.myVs = "my number";
........
........
........
int i;
int s;
i = Global.myVi;
s = Global.myVs + " is " + Global.myVi;