<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="akraj.snow.test.MainActivity"
tools:showIn="@layout/activity_main">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
Moving From One Activity to Another Activity
Start Another Activity
You can start another activity within the same application by callingstartActivity(), passing it an Intent that describes the activity “class name” you want to start.
An intent contains the action and optionally additional data. The data can be passed to other activity using intent putExtra() method.
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
The complete sendMessage() method.
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this,SecondActivity.class);
EditText editText = (EditText) findViewById(R.id.message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Receive Data
You can get theIntent that started your activity by calling getIntent() and retrieve the data contained within the intent.
- In the res/layout directory, edit the
content_second.xmlfile. - Add an
TextViewelement to the Layout.
Tip:<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" />android:textSize="20sp"for Change Text Sizeandroid:textColor="@color/blue"for Change Text Colorandroid:textStyle="bold"for Change Text Style - In the
java/akraj.snow.testdirectory, edit theSecondActivity.javafile. - Get the intent and assign it to a local variable.
Intent intent = getIntent(); - Extract the message delivered by
MainActivitywith thegetStringExtra()method.
Tip: In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Passing Multiple Values
MainActivity:-
Intent i = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Name", "Akshay Raj");
bundle.putString("Email", "akshay@snowcorp.org");
i.putExtras(bundle);
startActivity(i);
SecondActivity:-
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("Name");
String email = bundle.getString("Email");
TextView textName = (TextView) findViewById(R.id.name);
TextView textEmail = (TextView) findViewById(R.id.email);
textName.setText(name);
textEmail.setText(email);
Complete Code
content_main.xml (res/layout)
MainActivity.java (java/akraj.snow.test)
package akraj.snow.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "akraj.snow.test.MESSAGE";
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
editText = (EditText) findViewById(R.id.edit_message);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
content_second.xml (res/layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="akraj.snow.test.SecondActivity"
tools:showIn="@layout/activity_second">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/textView" />
</RelativeLayout>
SecondActivity.java (java/akraj.snow.test)
package akraj.snow.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}
}
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="akraj.snow.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="akraj.snow.test.MainActivity" />
</activity>
</application>
</manifest>
You can now run the app. When it opens, type a message in the text field, and click Send. The second activity replaces the first one on the screen, showing the message you entered in the first activity.
