2

I have value strUser and KEY ,I want to send this value to multiple different classes because this value will be used in 5 classes for url, I know the way to send value to one class only using Intent.putExtra as bellow :

Intent policy= new Intent(LoginActivity.this,EpolicyListPolis.class);
        policy.putExtra("etUser",strUser);
        policy.putExtra("key",KEY);
        startActivity(policy);

How can I send this value to multiple different classes at a time? can i use SharedPrefences..? how the way i write sharedPrefences in class and my destination class?

Aoyama Nanami
  • 2,532
  • 9
  • 32
  • 50

9 Answers9

5

You can use SharedPreferences.

For storing values into SharedPreferences:

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("etUser", strUser);
editor.putString("key",KEY);
editor.commit();

Just paste these lines before calling startActivity(policy);

And get values from SharedPreferences

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
String etUser1 = settings.getString("etUser", null);
String key1 = settings.getString("key", null);

And paste these lines at where you want etUser1 and key1. You can access this SharedPreferences value in any Activity. If you cant please take a look on here. It may help you.

I hope this will help you.

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
2

Try this...

send value from this class with Intent....

Intent policy= new Intent(LoginActivity.this,EpolicyListPolis.class);
    policy.putExtra("key",strUser);
    startActivity(policy);

this works like: KEY-"key" and VALUE-strUser. and you get this value from another class using KEY.

Get value like this.

String user = getIntent().getExtras().getString("key",null);

in user you get strUser value. and if strUser not pass any value than default null(right side of "key") asign for user....

same you use from all class you need... but remember all works with KEY...

because KEY(one type of ID) is only which is works to Put and Get particular value.....

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • all that i want is send value strUser and KEY into different class more than one class – Aoyama Nanami May 14 '13 at 06:22
  • your Intent redirect to only one class at a time and sequentially you use this for all class you need... both value pass by putExtra() and get by getExtra(). – Mr.Sandy May 14 '13 at 06:29
  • more detail about getExtra() check..http://developer.android.com/reference/android/content/Intent.html#getExtras%28%29 – Mr.Sandy May 14 '13 at 06:35
  • and about putExtra() check...http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.lang.String%29 – Mr.Sandy May 14 '13 at 06:36
0

If you are using dependency injection create a Model which hold your states in value objects. Then inject this model in your activities.

Or simply create a Singleton State object which can be referenced from anywhere.

Fuat Coşkun
  • 1,045
  • 8
  • 18
0

You have to just get these things in the other activity--

   Bundle bundle = getIntent().getExtras();
   String user = bundle.getString("etUser");
   String key = bundle.getString("key");

Or,

you can set these values in a class and get from them whereever you need.

Hulk
  • 2,565
  • 16
  • 24
0

I'm not really sure what you want to do, but IMO, if i will use a variable from one class to another, i will make the variable static and "get" the value of that variable from the class..

for example in one class i have a variable:

public class Login extends Activity {

static String username;

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.login_layout);

       username = "Your Name";
    }

and in another class, i would get that value like this;

   String var;
   var = Login.username;

but as i've read, this is not the best practice. Sometimes it just sufficient.

0

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

If your extra data is represented as strings, then you can use intent.getStringExtra() method.

String etUser = intent.getStringExtra("etUser");
String key = intent.getStringExtra("key");

Refer this for more details.

Bishan
  • 15,211
  • 52
  • 164
  • 258
0

I would like to tell you to use Application class. The better idea to get the reference from the following link

maintain the state of all the variables from the Application class

Bettter to go for this Application b'coz you can access all the variables in every class with out sending from intent.

This will help you.

Arpit Patel
  • 1,561
  • 13
  • 23
0

Application Object are some way, i would go for EventBus (like OTTO from Square or Greenrobot). Do everting on Application Context tend to create the "GOD Object" problem after quite some time. You should avoid this. SharedPreferences are also some good option.

Kitesurfer
  • 3,438
  • 2
  • 29
  • 47
0

you can use Bundle for pasing data form one Activity class to other Activity class Like this

  Bundle bundle = new Bundle();
    bundle.putString("Id", videoChannelId);
    bundle.putString("C/V", "C");
    bundle.putString("mode", "ch");
    bundle.putString("code", "LiveTV");
    bundle.putString("urlcount", "2");
    Intent intent = new Intent(First.this,Second.class);
    intent.putExtras(bundle);
    startActivity(intent);

Get the data in the second class like this by giving the bundle id

     Bundle  getBundle = this.getIntent().getExtras();

     name = getBundle.getString("Url") 
      etc......
Bebin T.N
  • 2,539
  • 1
  • 24
  • 28