Monday , December 10 2018
Home / Android / Passing data from one Activity to another in Android

Passing data from one Activity to another in Android

You can start another activity within the same application by calling startActivity(), passing it an Intent that describes the activity you want to start. Also you can pass some data to the activity “to be started”, using the Intent object too.

(1)  res/layout/activity_main.xml
<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: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=”.MainActivity” >

<TextView
android:id=”@+id/tvHelloWorld”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/hello_world” />

<Button
android:id=”@+id/btnPassData”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/tvHelloWorld”
android:layout_marginTop=”26dp”
android:layout_centerHorizontal=”true”
android:text=”Pass Data to Another Activity” />

</RelativeLayout>

(2) res/layout/activity_another.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<TextView
android:id=”@+id/tvMessage”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</LinearLayout>

(3) src/com/w2class/android/ActivityMain.java

package com.w2class.android;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

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

btnStartAnotherActivity = (Button) findViewById(R.id.btnPassData);

btnStartAnotherActivity.setOnClickListener(this);
}

@Override
public void onClick(View view) {

// 1. create an intent pass class name or intnet action name
Intent intent = new Intent(“com.w2class.android.ANOTHER_ACTIVITY”);

// 2. put key/value data
intent.putExtra(“message”, “Hello From MainActivity”);

// 3. or you can add data to a bundle
Bundle extras = new Bundle();
extras.putString(“status”, “Data Received!”);

// 4. add bundle to intent
intent.putExtras(extras);

// 5. start the activity
startActivity(intent);
}

}

(4)  src/com/w2class/android/AnotherActivity.java

package com.w2class.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AnotherActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);

// 1. get passed intent
Intent intent = getIntent();

// 2. get message value from intent
String message = intent.getStringExtra(“message”);

// 3. show message on textView
((TextView)findViewById(R.id.tvMessage)).setText(message);

// 4. get bundle from intent
Bundle bundle = intent.getExtras();

// 5. get status value from bundle
String status = bundle.getString(“status”);

// 6. show status on Toast
Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
toast.show();
}
}

About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply