In my application I need a variable from one activity to another activity without using any intent. So I have declared that variable as static and used as FirstActivit
[Edited]
Since you're saying that there is a value returned from this line getIntent().getExtras().getString("StockName")
, then try this code:
public class Detail extends Activity{
public static String stringValue; //make it public and static
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockdetail);
stringValue = getIntent().getExtras().getString("StockName");
}
Now access the static object in Table class:
public class Table {
Context c1;
Cursor c;
public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " + Detail.stringValue;
}
This should work properly. Make sure you're accessing the stringValue
variable after the Detail
activity is created.
[Original Answer]
Try this:
public class Detail extends Activity{
public static ApplicationClass ac; //make it public and static
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockdetail);
ac=new ApplicationClass();
ac.setStockName(getIntent().getExtras().getString("StockName"));
}
Now access the static object in Table
class:
public class Table {
Context c1;
Cursor c;
public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = "
+ Detail.ac.getStockName();
}
P.S. To access the static object/variable, follow this syntax:
Class_Name.Object_Name.Method_Name();
You should define your subclassed application class in your manifest. And you should never call "new ApplicationClass()". You can get a reference to ApplicationClass instance using activity's getApplication() method.
Detail.java:
public class Detail extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockdetail);
ApplicationClass app = (ApplicationClass)getApplication();
app.setStockName("blah");
}
}
Table.java
public class Table {
public String selectDate;
public Table(Activity a)
{
ApplicationClass ac=(ApplicationClass)a.getApplication();
selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = "
+ ac.getStockName();
}
Instantiate Table.java
public NewActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Table t = new Table(this);
}
}
Try to initialize firstly your class. But what I see you want to have some application context that is accessible via application. For that porpouse you can simply use that method but data try to keep in SharedPreferences
. So simply when you get sth from ApplicationClass
you simply get it firstly from shared preferences and return. :) And each time when you need your ApplicationClass you initialize it and there methods run shared preferences to get data.
public class Detail extends Activity{
ApplicationClass ac = new ApplicationClass();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockdetail);
ac=new ApplicationClass();
ac.setStockName(getIntent().getExtras().getString("StockName"));
}
Shared preferences context class.
public ApplicationClassWithSharedPreferences{
private Context context;
public ApplicationClassWithSharedPreferences(Context c){
context = c;
}
public String getSomeValueFromContext(){
SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE);
String highScore = sharedPref.getString("KEY", "DEFAULT");
return highScore;
}
}
Try this.
Step 1: Create a static Bundle object in Application class.( ApplicationClass.java)
E.g :
public static Bundle mMyAppsBundle = new Bundle():
Step 2:
Set key values pair in that bundle from anywhere. like this:
ApplicationClass.mMyAppsBundle.putString("key","value");
Step 3:
Now you can get these values from anywhere like this way:
String str = ApplicationClass.mMyAppsBundle.getString("key");
Please apply null check before using bundle objects for safety points of view.