Tuesday, 6 January 2015

Pass data between activities in Android

Continuing on from using Intents to Start a new Activity in Android from my last post I will be passing data between activities using Intent. I will be passing string data with Intents however a collection of other data types are also possible. See the Intent API for full list.

I have the same setup from the last post with two activities, but this time I will add an Edit text field into the activity_main.xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:hint="Edit text messaga"/>
    
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/editText"
        android:text="Start new Activity"/>

</RelativeLayout>

In the MainActivity.java I have used the putExtra() method to pass string data to the SecondActivity.class.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText editText = (EditText) findViewById(R.id.editText);
        
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("MESSAGE", editText.getText().toString());
                startActivity(intent);

            }
        });
    }
}

In the SecondActivity.java I have used the getIntent() method to retrieve the data from the Intent in the MainActivity.java file. I also have a Textview in the activity_second.xml layout with the id “secondActivityText” which will display the text from the EditText.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();
        String message = intent.getStringExtra("MESSAGE");

        TextView textView = (TextView) findViewById(R.id.secondActivityText);
        textView.setText(message);
    }

}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP