Are you new to Android Programming? Want to pass data or a string from one activity to another activity? If yes, keep reading. Here we will use INTENT to pass the data among various activities. Before jumping to the example, i would like to brief, that What INTENT is?

What is INTENT?

INTENT, the world itself explains that an intention to do something. Intent is an messaging object used to make communication possible among activity, services, broadcast receiver, content providers). Intent is generally based on three things:

  1. To Start Activity
  2. To Start Service
  3. To deliver Broadcast or Message

Generally there are two types of INTENT:

  1. Implicit Intent : Implicit Intent is unknown, so it is name less. Suppose you wanna share something, than it will scan the available apps from your Android Device and pop up with option.
  2. Explicit Intent: Explicit Intent is known, and has name. So it can be called directly with name. Suppose to start camera in Android.


Now lets come to the example with source code. Here, i have used Android Studio in the given example. I have created a project name INTENT in the given example.

 1) MainActivity.java

package com.app.my.intent;

import android.content.Intent;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

String a="Hello!, Have a nice day";  // I want to pass this string to the another activity

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

Button click = (Button) findViewById(R.id.button); // Here i have defined the button

click.setOnClickListener(new View.OnClickListener() {  //Click event on button

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, Main2Activity.class); //here defining the intent

//to pass data from Mainactivity.java to Main2Activity.java

intent.putExtra("e1", a); // Here putting an extra text in e1

startActivity(intent); //calling the other activity

}

});

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

2) content_mail.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto" 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"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me"

android:id="@+id/button"

android:layout_marginTop="143dp"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

</RelativeLayout>

3) Main2Activity.java

package com.app.my.intent;

import android.content.Intent;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

String e1; //defined an string variable e1

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

TextView tv = (TextView) findViewById(R.id.inte); //defined textview

Intent intent =getIntent(); // code to get data from previous activity

e1= intent.getStringExtra("e1"); // storing the data in variable e1

tv.setText(e1);// setting the data to be displayed in textview

Toast.makeText(getApplicationContext(), e1, Toast.LENGTH_LONG).show();//also displaying as a toast

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

}

4) content_main2.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto" 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"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:showIn="@layout/activity_main2" tools:context="com.app.my.intent.Main2Activity">

<TextView

android:id="@+id/inte"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</RelativeLayout>

Here, i am also providing the video tutorial for the above example. Refer video for knowing how things are done.

Hope you enjoyed. Share with your friends.

Keep sharing. Subscribe for sure. Stay tuned to Tech Tunes.

Follow me on Twitter