Passing strings between activities in android

Couple of scenarios:

  1. If you want to pass the string when you start the new activity, then add it to the starting Intent and retrieve it in the new activity's onCreate.
    Sending arrays with Intent.putExtra

    // Sending activity  
    String latLong = "test";  
    Intent i = new Intent(sendingClass.this, receivingClass.class);  
    i.putExtra("latLong", latLong);  
    startActivity(i);  
    
    // Receiving activity  
    Bundle extras = getIntent().getExtras();  
    String latLong = extras.getString("latLong");  
    
  2. If you want to pass the string when you return from an activity, then use startActivityForResult and implement the onActivityResult event
    http://micropilot.tistory.com/1577

  3. The 3rd scenario, passing the string between two activities running at the same time is not possible because only one activity can run (be in the foreground) at a time.


Sometimes, Intents become too cumbersome and annoying, instead I use an easier (maybe not optimal) design pattern: the Singleton. A singleton works like a common storage box reachable by code that sits anywhere in your app, where values are stored while the app's lifecycle is active. You can also put methods there. A singleton is a class that can only be instantiated once, and can be used as your one stop warehouse for all the variables that you need to access from everywhere. You can set/get any variable on the singleton from any activity or class, even context! As I said maybe there are better options, but I don't have the time to be punishing myself with intents, null pointers and what not. Create a new class with the following code, call it mySingleton or whatever, and start setting/getting variables from everywhere!:

public class MySingleton extends Application{ 
    private volatile static appSingleton mInstance = null;
    private String mystring;   

private appSingleton(){  
   mystring="hello"; //initialize your var here
   //Add all the variables you need, here.
public static MySingleton getInstance(){  //Singleton's core
        if(mInstance == null){
            mInstance = new MySingleton();
            }
        return mInstance;
        }

//Place Set and Get methods here
public String getMystring(){return this.mystring;}
public void setMystring(String s){mystring = s;}
//Add get/setmethods for your other variables here
} //Thats it

Now, lets say you want to set mystring to "goodbye" on Activity B, then do want to do this:

MySingleton.getInstance().setMystring("hello");

If you want to access "mystring" from ANY other Activity, class, etc... and display it on a textbox just do this:

MyTextBox.setText(MySingleton.getInstance().getMystring());

As you can see, you can write values anywhere and read those values from anywhere, with a single line of code. Enjoy!


In your LocationActivity class:

Intent i = new Intent(this, FindAndroidActivity.class);
i.putExtra("KEY",YourData);

In FindAndroidActivity class

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("KEY");
}