Android: transferring value between actions

In my Android app, I have to use a common string value for all actions. "commonValue" is the common string value that I want to use in all actions. Corresponding main action code:

public class TestActivity extends Activity { public String commonValue;//THE COMMON STRING FOR ALL ACTIVITIES /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); commonValue = "DemoValue"; } } 

In my next step, I created an object of the TestActivity class and tried to assign the string "testValue" to another string named "str"

 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testlist); TestActivity obj = new TestActivity();//OBJECT OF MAIN ACTIVITY String str = obj.commonValue; } 

but the value of "str" ​​in the second activity is not equal to the value assigned in my first action. Why is this and how can I do this?

Thanks!

+4
source share
8 answers

Try it -

TestActivity.java

 public class TestActivity extends Activity { public static String commonValue;//THE COMMON STRING FOR ALL ACTIVITIES /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); commonValue = "DemoValue"; } } 

another action

 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testlist); String str = TestActivity.commonValue; } 
+1
source

Put your value in string.xml

  <string name="common_value">DemoValue</string> 

and use in any activity like this.

 String common_value = getApplicationContext().getString(R.string.common_value); 
+4
source

Start using SharedPreferences in your application.

In the first exercise you did

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("commonValue", "DemoValue"); editor.commit(); 

In your second action

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); String str = settings.getString("commonValue", null); 
+3
source

If the value is always the same, you can create a public static final variable and access it through TestActivity.COMMON_VALUE.

If you want to pass a value between actions, you must use Intent and add an extra value with the value you want to pass.

+1
source

As Sana suggested, use SharedPreferences.

Alternatively, use a global constant class. If you want to stick with what you have, you can try: String str = TestActivity.this.commonValue;

Your existing code creates a new instance of the action, so it will not have the value that you set.

0
source

Use the Bundle to transfer data between actions. and methods

 intent.putExtra() 

and if you want the data to be global for your application, then create an application class and save the data there.

0
source

We have an application file for each application, where you can declare a variable there and how the application file can get from any activity, so use the public setter to get / set what

there is an irrefutable argument that you can submit as a link to developer.android http://developer.android.com/resources/faq/framework.html

 Singleton class A public static field/method A HashMap of WeakReferences to Objects (almost same as my above solution ) Persistent Objects 

take a look at them also

0
source

The reason commonValue does not match what you set in the TestActivity onCreate method is because this function has not yet been called.

The solution for this has already been mentioned by others. How to put value in a bunch.

0
source

All Articles