How to switch between screens with data in android

Hi
Here we will see some tricks and techniques for switching between Screen in Android. For 1st example I consider you have two activity name FisrtActivity.java and SecondActivity.java

First activity have a button and on clicking that the screen will switch to SecondActivity. so the code will be for this:

    Intent nextScreen = new Intent(getApplicationContext(), 
        SecondActivity.class);
    startActivity(nextScreen);

So this code is loading the second screen on click, so the full code should look like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // This is the layout for the first screen. File should place 
        // in layout folder named firstscreen.xml (considering you 
        // know how to create a layout, If not please find my another 
        // article for that..)
        setContentView(R.layout.firstscreen);  

        Button goToSecScreen = (Button) findViewById(
               R.id.btnGoToSecScreen);
        goToSecScreen.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View arg0) {
                //Starting a new Activity
                startActivity(new Intent(getApplicationContext(), 
                      SecondActivity.class));
 
            }
        });

For next examples I am considering that you will be able to handle the on click part. Ok now we will see how to send data to second activity:

Btw, the following code

    startActivity(new Intent(getApplicationContext(), 
         SecondActivity.class));

can be written as bellow:

    Intent secondScreen = new Intent(getApplicationContext(), 
           SecondActivity.class);
    startActivity(secondScreen );

These tow lines does the same thing as above.

So now, we will send some data to the second activity. so first activity code will be like this:

    Intent secondScreen = new Intent(getApplicationContext(), 
           SecondActivity.class);
    
    //adding data to the intent to send to second screen as well
    secondScreen .putExtra("name", "my name");
    // this code if you have text field associated this var inputName
    /*secondScreen .putExtra("name", inputName.getText().toString());*/

    secondScreen .putExtra("email", "my email");
    // this code if you have text field associated this var inputEmail
    /*secondScreen .putExtra("email", inputEmail.getText().toString());*/

    startActivity(secondScreen );

So, it sends the data to second screen, now we have to catch the data. so the following code have to write in onCreate method in SecondActivity.java file:

    Intent firstScreen = getIntent();
    
    // Receiving the Data
    String name = firstScreen.getStringExtra("name");
    String email = firstScreen.getStringExtra("email");
    Log.e("Second Screen", name + "." + email);
 
    // Displaying Received data, assuming you have these two text field in second screen
    //txtName.setText(name);
    //txtEmail.setText(email);

So, if my case is I also want that the second activity also will some data to the first screen then just wirte in the FirstActivity as

    // 2 is the which will be mention in result
    startActivityForResult(intent, 2);  

instead of

    startActivity(secondScreen );

override the implementation of “onActivityResult()” method. So in FirstActivity should have this code:

    @Override
    protected void onActivityResult(int requestCode, 
              int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 2 is the same ocde that you send to the second 
        // screen when loading..
        if(requestCode == 2){  
 
            // Receiving the Data
            String name = data.getExtras().get("name");
            String mail = data.getExtras().get("mail");
            Log.e("First Screen", name + "." + mail);
 
            // Displaying Received data, assuming you have these 
            // two text field in first screen
            //txtName.setText(name);
            //txtEmail.setText(mail);
        }
 
    }

and the second screen should have this code in the button click to response the result to the first screen

    Intent firstScreen = getIntent();
    firstScreen.putExtra("name", "AndroidHive.info");
    // this code if you have text field associated this 
    // var inputName
    /*firstScreen.putExtra("name", 
           inputName.getText().toString());*/

    firstScreen.putExtra("email", "AndroidHive.info");
    // this code if you have text field associated this
    // var inputEmail
    /*firstScreen.putExtra("mail", 
           inputEmail.getText().toString()); */ 

    // Setting resultCode to 2 to identify on old activity
    setResult(2,firstScreen);

    // now close the second screen
    finish();

So? what we have seen? for just load an activity we will call startActivity(). For getting data back from the loaded activity we will call startActivityForResult() with result code to identify this result came from which call and will override and implement onActivityResult() method. In this case we will hust call the finish() method to return back to the caller Activity.

I Hope you will be able to write down the full code. If not then please fork this from github: https://github.com/behestee/ActivityInteraction