Reputation: 1163
I have one class ApplicationDetails, with getter and setter methods.
public class ApplicationDetails {
String supportURL;
String companyURL;
String copyRightText;
// with getter and setter methods
}
I am setting all data in my splash screen activity.
ApplicationDetails appDetails = new ApplicationDetails();
String supportURL = getResources().getString(R.string.support_url);
appDetails.setSupportURL(supportURL);
For sample I just setting data from string file but in app its coming from different sources.
But when I tried to access data in different activity its returns null value. e.g.
public class AboutViewController extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApplicationDetails appDetails = new ApplicationDetails();
System.out.println(" app support url " + appDetails.getSupportURL());
}
}
output
I/System.out(2242): app support url null
any help.
Upvotes: 1
Views: 4786
Reputation: 30855
Update
As you setting value in Splash screen that object in memory was different and in another activity you creating another object that also different in memory that's why you getting null.
If this was your requirement to init url in splash screen and used in another then there are many ways.
Edited
For using single object you need make that object declare as public static in splash screen
public static ApplicationDetails appDetails;
now assign value in splash screen oncreate() and used in another activity or even another class also like this way
public class AboutViewController extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// direct use object by class name
System.out.println(" app support url " + SplashScreen.appDetails.getSupportURL());
}
}
Upvotes: -2
Reputation: 1163
Thanks all for all suggestions. Now I am using only one instance of a class.
public class ApplicationDetails {
private static ApplicationDetails instance = null;
String supportURL;
String companyURL;
String copyRightText;
// with getter and setter methods
public static ApplicationDetails getInstance() {
if (instance == null) {
instance = new ApplicationDetails();
}
return instance;
}
}
And I am setting and getting like this
ApplicationDetails appDetails = ApplicationDetails.getInstance();
appDetails.setSupportURL(supportURL);
and in activity
ApplicationDetails appDetails = ApplicationDetails.getInstance();
appDetails.getSupportURL();
Its wrks fine.
Upvotes: 0
Reputation: 1875
You get null, because you create a new object and all fields are initialized to zero.
In your case, I see these fields are going to be the same through application, so you can use a Singleton pattern and instantiate only one object for your application and refer to it later on. You don't need to create a new object each time you refer to it. It would be ok for this class and you can also make them constants. (I guess these variables won't change through execution)
Upvotes: 5
Reputation: 7849
You can use the shared preference to store data to be used through your application. Here the Context in the constructor is nothing but your Activity.
public class ApplicationDetails {
public static final String SUPPORT_URL = "support_url";
public static final String COMPANY_URL = "company_url";
public static final String COPYRIGHT_URL = "copyright_url";
String supportURL;
String companyURL;
String copyRightText;
private Context context;
public ApplicationDetails(Context context) {
super();
this.context = context;
}
private String getPreference(String key)
{
return PreferenceManager.getDefaultSharedPreferences(context).getString(key, null);
}
private void setPreference(String key, String value)
{
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).commit();
}
public String getSupportURL() {
if(supportURL == null)
supportURL = getPreference(SUPPORT_URL);
return supportURL;
}
public void setSupportURL(String supportURL) {
this.supportURL = supportURL;
setPreference(SUPPORT_URL, supportURL);
}
public String getCompanyURL() {
if(supportURL == null)
supportURL = getPreference(COMPANY_URL);
return companyURL;
}
public void setCompanyURL(String companyURL) {
this.companyURL = companyURL;
setPreference(COMPANY_URL, companyURL);
}
public String getCopyRightText() {
if(copyRightText == null)
copyRightText = getPreference(COPYRIGHT_URL);
return copyRightText;
}
public void setCopyRightText(String copyRightText) {
this.copyRightText = copyRightText;
setPreference(COPYRIGHT_URL, copyRightText);
}
}
Upvotes: 0
Reputation: 695
As fast solution you can make your supportURL object static, but this isn't good solution.
public class ApplicationDetails {
static String supportURL;
static String companyURL;
static String copyRightText;
// with getter and setter methods
}
better solution is to pass strings from one activity to another with intents, when you are starting your AboutViewController Activity.
Upvotes: 0