How to share data between different android activities?

As mentioned in the Android Developer documentation "An activity is a single, focused thing that the user can do". This implies that for different unrelated things you should have different activities. However when you are switching activities and the new upcoming one may need some data/information of what has transpired before to decide what happens next. In such cases we need a way to share data between activities. Here we will create a simple app which will take user's name as an input in the first activity and say Hello on the next activity. This can be done in a single activity but my aim is just to demonstrate the passage of information from one activity to other , rather than building a beautiful app right now.

For this demo/tutorial you will need Android Studio as your environment. You can use eclipse or any other IDE after understanding the code but I will be using the most widely used IDE i.e Android Studio. I have selected API17 (Android 4.2 Jelly Bean) as the minimum supported SDK. You can select one of your choice. Post this you select an Empty Activity to start with. If you have kept the default name for the activity then you should now have an App project created with a MainActivity.java class and an activity_main.xml layout file.

We will dive directly into the layout file. Open it in the text mode and not the design mode. There will be a TextView in there. Add an EditText as well which will be used to take input from user. Post this add a Button. On the click of this you would start the new activity and show the user's name there.

Beginner Info : An EditText is a widget which can take text input from the user while a TextView is one which shows information to user.

We have used RelativeLayout, you can use Layout of your choice. The activity_main.xml looks something like this. Don't forget to give an id to your EditText

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="example.com.datasharingdemoapp.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="22sp"
        android:textAlignment="center"
        android:text="Enter your name"
        android:id="@+id/tv_input_message"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_input_message"
        android:layout_centerHorizontal="true"
        android:hint="Your name here"
        android:id="@+id/et_inputName"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btSubmit"
        android:text="Submit"
        android:layout_below="@id/et_inputName"
        android:layout_centerHorizontal="true"
        android:onClick="sendMessage"/>
</RelativeLayout>

Notice the line 

android:onClick="sendMessage"

This is the invocation of some action on the click of the button. We will come to the declaration/definition of the sendMessage method in a while.

Let's move to the MainActivity.java

You would need to find the EditText view by its id and store it in an instance variable. Do this in the onCreate method. We now need another activity created. Let's name it DisplayActivity. Add a TextView and give it an id.

Let's come back to the MainActivity class and create the sendMessage method. Now Android has a specific way to declare the onClick methods. The should take only View as an argument else the binding will not happen. In this sendMessage method, we will create an Intent with the DisplayActivity and add the text entered in the EditText, using the method putExtra with a pre-defined attribute name. It keeps this in a key value pair which can be extracted by the upcoming activity. Then you start the activity passing the intent with it. Before we move on to DisplayActivity, let's take a look at the final look of the MainActivity class.

Your final MainActivity looks like this.

package example.com.datasharingdemoapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    public static final String MESSAGE_NAME="name";
    EditText etNameInput=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etNameInput=(EditText)findViewById(R.id.et_inputName);
    }

    public void sendMessage(View view){
        Intent intent = new Intent(this, DisplayActivity.class);
        String message = etNameInput.getText().toString();
        intent.putExtra(MESSAGE_NAME, message);
        startActivity(intent);
    }
}

Let's come to DisplayActivity.java

Here you just need to get the String message which was passed with Intent to this activity and set it to the textview you created for DisplayActivity. For getting the intent you will use the getIntent method and from the intent you will get the passed message using the same pre-defined attribute name. Now get the TextView using the id you gave to it in the activity_display.xml and set this passed message to it's text value.

Your final DisplayActivity looks like this.

package example.com.datasharingdemoapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        Intent intent=getIntent();
        String name=intent.getStringExtra(MainActivity.MESSAGE_NAME);
        TextView tvShowName=(TextView)findViewById(R.id.tv_show_name);
        tvShowName.setText(name);
    }
}

Now is the time to test run the app. You can use any Android Virtual Device or an actual device (after connecting to your android studio) to test this app. My app looked like this when I tested it on Nexus 5 AVD

Sharing Data between activities

core java 12 Android 12

FOLLOW US ON LinkedIn



Explore Tutu'rself