Android Development – Passing Data Between Screens

By | May 6, 2010

In this short post we’ll be going over how to pass simple data between screens in an Android app. We’ll be working with the sample app we developed in a prior post, Adding Screens & Button Handlers.

Scenario

In most Android applications, you’ll want to pass state information from screen to screen. This information could be as simple as an indicator of what button was pressed to move to the new screen, to more complicated state based on multiple entries in form fields on the prior screen. We will be going over the more simple case here, showing how to indicate to the next screen what button was pressed on the prior screen.

Our sample application has three screens, shown below in sequence.

screen1 screen2 screen3

We are going to pass along an identifier to the next screen indicating what screen we are arriving from.

Implementation

To pass information along from a screen, you use the putExtra method defined on the Intent object you pass in to the startActivity call that starts the next screen. To let the second screen in our sequence above (which fantastically enough, has a TextView with “Screen 1” in it) know we are arriving there from the main screen, we will pass the text “main screen” with the key “ARRIVING_FROM”, as shown below.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button mainNext = (Button) findViewById(R.id.nextScreenMain);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.setClassName("com.screenssample", "com.screenssample.screen1");
            i.putExtra("ARRIVING_FROM", "main screen");
            startActivity(i);
        }
    });
}

The magic is happening on line 10, and there’s really not much to it.

Now we modify the code in our second screen to pull our information out and display it in a TextView.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);

    Intent i = getIntent();
    Bundle b = i.getExtras();
    String newText = b.getString("ARRIVING_FROM");

    TextView title = (TextView) findViewById(R.id.TextView01);
    title.setText("arriving from " + newText);

    Button mainNext = (Button) findViewById(R.id.nextScreen1);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.putExtra("ARRIVING_FROM", "screen 1");
            i.setClassName("com.screenssample", "com.screenssample.screen2");
            startActivity(i);
        }
    });
}

Lines 5-7 are where we pull the information that was passed from the main screen. The getIntent method is off of the Activity class, and allows us to call getExtras, which returns to us a Bundle that contains any information that was stuffed into our screen’s intent. We then use the same key that we used in the main screen to store the information to pull the information out. A Bundle class has lots of different ways to pull information out, consult the corresponding section in the Android SDK documentation to learn more.

At this point we can fire up our application and see how it behaves. The main screen looks the same as before, but when we get to the second screen…

screen2postMod

Pretty straightforward stuff. In our example we were passing a string value, but you can pass a much wider variety of objects using this method – anything that can be held in a Bundle object. Again, consult the Android SDK documentation to learn more. In future posts we’ll go over other ways of persisting information, such as preferences, files, and databases.