0

I have created two activities in my android application.

The variable is declared in the activity 1. Now I want to use that variable, update it's values in activity 2 as well as activity 1 simultaneously. And the activities should use the latest values for that variable.

I guess we can do this using Intents, but I want to know any other simpler method.

harpun
  • 4,022
  • 1
  • 36
  • 40
  • 1
    use shared preferences – Raghunandan Oct 06 '13 at 11:31
  • What is the variable? – Szymon Oct 06 '13 at 11:34
  • Use shared preference as suggested or use static variable in a utility class in case its singleton instance. I usually keep following variables static: `DateFormatter`, reuseable Gson object, Custom loaded Fonts etc. Anything that won't cause memory leak and can be singleton. – M-Wajeeh Oct 06 '13 at 11:38

5 Answers5

2

You can use SharedPreferences

To retrieve data from shared preference

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) 
{
  int selectionStart = prefs.getInt("selection-start", -1);
  int selectionEnd = prefs.getInt("selection-end", -1);     
}

To edit data from sharedpreference

 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();
Basbous
  • 3,927
  • 4
  • 34
  • 62
1

The android documentation is good place to start:

http://developer.android.com/guide/faq/framework.html#3

  Singleton class

You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
SBerg413
  • 14,515
  • 6
  • 62
  • 88
0

You can declare this variable on your custom Application class as you can get to the application from each activity by calling getApplication() method.

public class YourApplicationextends Application {

    public String yourVariable;

    // the rest of the code

}

You can declare your custom application class in AndroidManifest.xml:

<application
    android:name=".YourApplication"
/>
Szymon
  • 42,577
  • 16
  • 96
  • 114
0

this is how you pass parameter between activities

Intent i = new Intent(getApplicationContext(), SecondClass.class);
                // passing array index
                i.putExtra("passThisParam", passThisParam);
                Log.d("log tag","param passed===>>>"+passThisParam);
                startActivity(i);

and to receive this parameter in next activity

Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("position");
-1

The easiest way (not the correct way) is to create a global variable accessible to both activities.

Example

Declare variable in one of the activities as public and static like this:

FirstActivity.java

public static String variable = "value";

In the other activity you can access and modify the variable like this:

SecondActivity.java

FirstActivity.variable = "newValue";

Now if you print the variable in any of those activities the value should be "newValue"

If you want to do this correctly you should consider a Singleton class, SharedPreferences or using Intents. It takes a little bit more of work, but in the end you have a more robust piece of code.

rosorio
  • 321
  • 2
  • 5