Passing primitive Data types between activities

After some time working on android projects, I discovered that it is actually very important to pass data between activities in most projects. It is rather simple when it comes to passing primitive datatypes.



To pass integers or strings, one could easily follow the normal path of calling another activity (jump from Activity1 to Activity2) using startActivity or startActivityForResult

The difference between the two is the simple design choice: whether Activity1 needs to know that Activity2 is closed. When this is a requirement (maybe to update something or to know whether Activity2 did it job or not),   you should use startActivityForResult. This function takes an Intent and an int that is an identifier of this activity. This identifier will help you know that the activity you just called Activity2 is closed.
To use this function and send primitive datatypes to Activity2 you can add to the Intent you created values of types: int, string, boolean, byte, char, CharSequence, double, float, long, and short using the function putExtra.
So you start with creating an intent that will jump to Activity2:
Intent intent = new Intent(Activity1.this, Activity2.class);

This intent now can receive extra data using the putExtra function. Each variable or data instance you want to send must have an identifier so that you can retrieve this value in Activity2.

intent.putExtra("theDataIdentifier", yourData);

Now you call the function startActivityForResult and besides the Intent you just created, you pass an integer which is the unique identifier, named requestCode, for this Activity. In this case the value will be 9090. You can choose any integer of course.

startActivityForResult(intent, 9090);

To put it all together:

Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("theDataIdentifier", yourData);
startActivityForResult(intent, 9090);

Now your Activity2 is started and to get this data in Activity2 all you have to is the following

String myData = getIntent().getStringExtra("theDataIdentifier");

If you have many values to send you just use in Activity1 more putExtra calls with different data identifiers and get them using the same way in Activity 2.

When Activity2 is dismissed maybe using the back button or when you call finish() in Activity2, the function protected void onActivityResult (int requestCode, int resultCode, Intent data) in Activity1 is called.
The requestCode will automatically be in this case 9090 and the resultCode is set in Activity2 using the function setResult(int resultCode) , finally data contains the data that Activity2 wants to send to Activity1. So for example if a user presses a button Exit in Activity2 which has the following code:

setResult(10);
finish();

You should have the function onActivityResult (int requestCode, int resultCode, Intent data) in Activity1 to receive this result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 9090)
{//this means that Activity2 is the one that finished (there might be more Activities)
if(resultCode == 10)
//this means that the user pressed the Exit button
//you might show him a message for example: Why didnt you answer the question or Why did you exit .. you see the point
}
}

Finally, if Activity2 needs to send some data back to Activity1. Instead of simply using a finish followed by a setResult, one could add an intent to the setResult function in Activity2:

Intent data = new Intent();
data.putExtra("myResultIdentifier", myDataValue);
setResult(11, data);
finish();

Now in the function onActivityResult, you can use the third parameter of the function to retrieve this result in the exact same way using  the getStringExtra/getIntExtra/getBooleanExtra ... functions:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 9090)
  {//this means that Activity2 is the one that finished (there might be more Activities)
    if(resultCode == 11) {
      if(data.getBooleanExtra("myResultIdentifier",false) { //false is the default parameter in case there is no extra called "myResultIdentifier"
        //bla bla 
      }
      else {
        //bli bla
      }
    }
  }
}

Now, Activity1 is able to send primitive data types to Activity2, and Activity2 is also able to send back primitive Data types.

One application would be a "Choose a date" button in Activity1 which takes you to Activity2 where a calender is shown. You could decide to send back a boolean that indicates whether the user canceled the operation or something. And you could also send the date back to Activity1 as a float value.

1 comment:

  1. We expect that the new LG V30 will be a super success when it launches at the upcoming global event. With the other Smartphone manufacturers going all in to find the proverbial Pandora’s Box when it comes to customer satisfaction, we have developed a wishlist of the features which the LG will supposedly carry. We can also predict that the MWC 2017 will not be the LG V30 showcase after all, as mentioned here. LG V30

    ReplyDelete