2

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 FirstActivity.a but this is returning so null, Hence I have created a class that extends application and declared that variable there still I am getting null. no clue how to achieve this.

Googled a lot but everyone are suggesting either to use static or extend Application class, unfortunately both are not working for me.

Application class:

public class ApplicationClass extends Application{ private String StockName; public String getStockName() { return StockName; } public void setStockName(String stockName) { StockName = stockName; } } 

Setting the variable in one activity as:

public class Detail extends Activity{ ApplicationClass ac; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stockdetail); ac=new ApplicationClass(); ac.setStockName(getIntent().getExtras().getString("StockName")); } 

Retriving the variable in another class as:

public class Table { Context c1; Cursor c; ApplicationClass ac=new ApplicationClass(); public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " + ac.getStockName(); 

I'm not sure how to achieve this.

Edit

public class Detail extends Activity{ public static sname; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stockdetail); sname=getIntent().getExtras().getString("StockName"); } public class Table { Context c1; Cursor c; public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " + Detail.sname; 
6
  • Why are you creating a new object (ApplicationClass ac=new ApplicationClass();) again in Table class? Commented Mar 31, 2014 at 10:56
  • Try to use spring for android :) Commented Mar 31, 2014 at 10:56
  • @JoelFernandes Is it not required? Sorry to ask but I am newbie to both and android and java.... Is it wrong way.. in that case how to use the variable in application class? Commented Mar 31, 2014 at 10:57
  • @Rafik991 Thanks for your reply.. how to use what is the process? I am newbie to android. I have done 80% of my application now if I use spring do I need to change the whole application? Commented Mar 31, 2014 at 10:59
  • @Siva Check my answer below on how you can access static objects/variables Commented Mar 31, 2014 at 10:59

4 Answers 4

3

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); } } 
Sign up to request clarification or add additional context in comments.

5 Comments

This means can I use getApplication().getStockname?
I am not getting any getApplication() in the target class
You need to pass the activity reference in the target class. <activity>.getApplication() should work.
I am bit confused here... target class where I am using this variable is not activity its just a class.. Table class in my question. In this case how to use the variable in application class
In your table class, add a setActivity method and store a reference to your activity.
2

[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(); 

8 Comments

Thanks for your reply but when I tried this way I am getting null pointer exception where I used Detail.ac.getStockName()
Is the Detail activity called before the Table class? Also, do check if you're getting any value in this getIntent().getExtras().getString("StockName")
Actually class where I want to use the application variable is not activity... I will use that in a string and will pass that string from table class to Detail(Activity) class
Ok my bad. Didn't see that you were extending ApplicationClass with Application. The reason why you're getting null is that Applicaton is called before any activity is started. What you can do is, instead of using ApplicationClass object, put the value of getIntent().getExtras().getString("StockName") in a static string variable and access the static variable in Table class.
result is same but this time there is no exception instead sname has returned null. Please see my edit in question
|
1

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.

18 Comments

Thanks for your response @Lavekush but where and how to set the value for the key? can you please elaborate a bit on this
There is no SetString.. do you mean put?
I have tried putString but still the result is same value is null in Value
Remember You just need to create bundle object in ApplicationClass only one.
setting values code: ApplicationClass.mMyAppsBundle.putString("SName", stockname); this is in one class where I am setting value.. now retriving the value in another class like this ApplicationClass.mMyAppsBundle.getString("SName")
|
0

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; } } 

5 Comments

Thanks for your reply.. how can I use shared preference... some insight on that...
Check my answer again ;) There is sample you can get more about SharedPreferences in developer.android.com/training/basics/data-storage/…
Ok can this be written in Application class
@Rafik991.. still banging my head with the issue... can you please where can I declare SharePreferences also how and where can I set and get values.
Read my answer again, I provided there sample get method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.