In this Android tutorial you will learn how to pass data between Android Activities. You will learn how to pass data forward from activity A to activity B and also how to return result from activity B to activity A. Let’s get started.
Pass Data From One Android Activity to Another (Forward)
To pass data from activity A to activity B use the following code snippet. Let’s assume you need to pass data from MainActivity to an Activity2.java file.
Intent activity2Intent = new Intent(getApplicationContext(), Activity2.class); activity2Intent.putExtra("extra_text", "Hello world"); startActivity(activity2Intent);
Note the use of putExtra() method. You can use it to pass Key -> Value pairs to your next activity.
Where:
- extra_text – is the key,
- Hello world – is the value.
Read Data
To read data in Activity2.java file use the following code snippet.
Intent intent = getIntent(); String extraText = intent.getStringExtra("extra_text");
Pass Back a Result
If you need to start an Activity which can then return back a result then when starting an activity use startActivityForResult() instead of startActivity(). Here is code example:
Starting a new activity when Button is clicked
package com.kargopolov.differentexamples; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { static final int RESULT_CODE = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buttonOne = findViewById(R.id.buttonOne); buttonOne.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { System.out.println("Button Clicked"); Intent activity2Intent = new Intent(getApplicationContext(), Activity2.class); activity2Intent.putExtra("extra_text", editText1String); startActivityForResult(activity2Intent, RESULT_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String messageReturned = data.getStringExtra("message_return"); System.out.println("Result code = " + resultCode); System.out.println("Message returned = " + messageReturned); } }
Notice a new method added onActivityResult(). This method will be called when Activity2 returns a result.
Returning a Result
To make Activity2 return a result together with some Extra information in an Intent follow the following code snippet.
Intent intentWithResult = new Intent(); intentWithResult.putExtra("message_return", "This data is returned when user click button in target activity."); setResult(1, intentWithResult); finish();
Here is a complete code from Activity2 that reads data passed from MainActivity and then returns data back to MainActivity.
package com.kargopolov.differentexamples; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); // Reading Data from Main Activity Intent intent = getIntent(); String extraText = intent.getStringExtra("extra_text"); System.out.println("Received data from Main activity = " + extraText); // Sending Result and Extra back to Main Activity Intent intentWithResult = new Intent(); intentWithResult.putExtra("message_return", "This data is returned when user click button in target activity."); setResult(1, intentWithResult); finish(); } }
I hope this short Android tutorial was helpful.