Reputation: 1804
I have an app with two activities and one utility class to server both activities. The main activity passed info to the utility class and then the second activity will access that class and get that info. The problem is that someway along the way, the string I was passing becomes a null when the second activity is trying to get that string. Here is the methods I'm using for this (if more info is needed please let me know).
The method in the main activity to pass info to the utility class:
public void setDateUT(int i){
String s = Integer.toString(i);
UT.getDate(s);
}
The method in utility class to receive this info (and set it into the date variable):
public class Utility {
String date;
public void getDate(String s){
this.date = s;
}
The method (and some extra info) in the 2nd activity to get this info back and set it in the TextView:
String C;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shifts);
TextView setDate = (TextView)findViewById(R.id.setdate);
setDate();
setDate.setText(C);
}
public void setDate(){
C = UT2.setDate();
}
The method to return date variable:
public String setDate(){
return date;
}
Can you tell me whats wrong? why is my string not passing correctly and I'm getting a null instead?
Upvotes: 1
Views: 493
Reputation: 1804
Thank you for all your comments, after doing some search and with your help (especially Kal) I realized what I need was my utility class to be a Singleton class. It works great now!
Upvotes: 1