Grant
Grant

Reputation: 4543

Android : Passing a value between activities

In my Android application I have to use common string value for all activities. "commonValue" is the common string value that I want to use in all activities. Relevant code of the main activity like this :

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 activity I created an object of "TestActivity" class and tried to assign "testValue" string 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 "str" value in second activity does not equal to the value assigned in my first activity. Why is that & How can I do this?

Thanks!

Upvotes: 4

Views: 272

Answers (8)

Praveenkumar
Praveenkumar

Reputation: 24476

Try this -

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 activity

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testlist);

    String str = TestActivity.commonValue;
}

Upvotes: 1

mariomario
mariomario

Reputation: 660

The reason why commonValue doesn't equal what you set in TestActivity onCreate method is because that function hasn't been called yet.

The solution for this is already mentioned by others. Like putting the value in a bundle.

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

We have an Application file for each app you can declare the variable there and as the Application file can get from any activity so using the public getter setter and can get/set that

there are vaious oter metjod you can sue as mention on 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 on them as well

Upvotes: 0

jeet
jeet

Reputation: 29199

To pass data between activities use Bundle. and methods,

intent.putExtra()

and If you want to set data to be global to your app, then create an application class, and save the data there.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33238

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

Upvotes: 4

BDFun
BDFun

Reputation: 551

As Sana has suggested, use SharedPreferences.

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

Your existing code is creating a new instance of the activity, so it's not going to have the value you had set.

Upvotes: 0

Tim
Tim

Reputation: 6712

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

If you want to pass around a value between to Activities you should use Intents and add an extra with the value you want to pass.

Upvotes: 1

Sana
Sana

Reputation: 9915

Start using SharedPreferences in your app.

In your first activity you would do

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

In your second activity

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

Upvotes: 3

Related Questions