How to send data to created activity on Android

I need to send data from EditText to action A in Activity B. I tried:

Intent intent1=new Intent(A.this,B.class); intent1.putExtra("fromA", "text"); startActivity(intent1); 

But this does not work, because activity B has android:launchMode="singleTask" and was created earlier.

How else can I send data?

+4
source share
3 answers

You override onNewIntent() in Activity B and get intent in this method.

As below code:

 @Override protected void onNewIntent(Intent i) { String s = i.getStringExtra("fromA"); } 

In the above code, you will get the s value from Activity A

+11
source

You can do it with your iPhone. Create a class where you can set data before starting a new action and access the same data from a new activity.

This will work as follows

  • There are two actions, FirstActivity and SecondActivity.
  • The data to send is the first and last name

therefore there will be one class in which you will have a variable for the data

  public class DataTransporter{ public static String firstName; public static String lastName; } 

In the first step, your code will be

  DataTransporter.firstName = "abc"; DataTransporter.lastName = "xyz"; Intent intent = new Intent(FirstActivity.this,SecondActivity.class) startActivity(intent) 

In the second step you can get this data

  @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); String first = DataTransporter.firstName; String last = DataTransporter.lastName; } 

You can delete data from the transporter class (if necessary) after retrieving.

+1
source

I prefer to use SharedPreferences to save my data and use it in all classes, plus they will be saved on the device, which will make them available even after the application is killed ... Here is an example for ya!

 //Some String that I should remember, I am just using the package name for now String app = this.getPackageName();/*This is going to be used more like a file to save my stuff to*/ //Setting our sharedpreferences SharedPreferences sha = sha = getApplicationContext().getSharedPreferences(app, SherlockActivity.MODE_PRIVATE); String myString = "This is the String that you want to save so you can use among your classes" //Now we call in an editor for that SharedPreferences so we can write and delete stuff from it . Editor edit = sha.edit(); //Now we insert our String. edit.putString("Something_you_can_remember" , myString);//You will need the "Something_you_can_remember" a few lines ahead , so remember it ! edit.apply(); //Or we can use edit.commit() , but I prefer apply() //Now our String is saved ! So lets read it ! String whatever = sha.getString("Something_you_can_remember" , "The String incase myString didn't even exist , saves you from a NullPointerException"); //Here we go ! Now we have our String saved and can be readable among the classes ! 

Alternatively, if you want to delete this line or whatever you put there, you can call

 edit.remove("Something_you_can_remember"); //or edit.clear() to remove all the values stored ! 

Hope this helps!

0
source

All Articles