Passing Intent data from Activity to Class

I want to pass a string from my activity class to my class that does not propagate to the activity. I know this is possible, but how do I do it? I am new to android development so please help

+3


source to share


2 answers


In android, you can send data via Intent or Intent followed by a Bundle:

Intent i = new Intent(current_activity.this, linked_class.class);
i.putextra("Key", value);

      

And get the value (let's say string value) in another class like:

String value = getIntent.getExtra("String key which you used when send value");

      

Option 2 :

class yourClass {
public static String _utfValue = "";
void sendValue(){
   _utfValue  = "some value";
}
}

      



And select this value in your java class, for example:

String value = A._utfValue ;

      

Option 3 . You can use SharedPreference to store a value and get it from another class.

Option 4 . You can use static method with some return value and select the method in your java class via class name.

All options are rough. So just check it out, hope one of them helps you.

+5


source


You create a static variable and access another class

as



Static String mydemo=null;
mydemo="sagar";

      

then access directly in another class

-1


source







All Articles