Simple Activity Change


The Activity class is a crucial component of an Android app, and the way activities are launched and put together is a fundamental part of the platform's application model. Unlike programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.

We will be suing Intents to change the activity and transfer the data between the activities.

An Intent is an object that provides runtime binding between separate components, such as two activities. An Intent can be used for many other things in an Android Application, but in our application we will be using an Intent for changing an activity and transferring data from ne activity to another.

Here' a simple application in which we take inputs from one activity and upon a button click event, a second activity starts which displays the values fed in first activity.

eg:


AndroidManifest.xml :

xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ourcoaching.simpleapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
intent-filter
>
activity>
<activity android:name=".SecondActivity">activity>
application>

manifest>


activity_main.xml :


xml version="1.0" encoding="utf-8"?>
<android.widget.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"
tools:context="com.ourcoaching.simpleapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Enter your details"
android:textAlignment="center"
android:textSize="30dp"/>
LinearLayout
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:hint="Name" />
android.support.design.widget.TextInputLayout>
LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/emailID"
android:hint="Email ID" />
android.support.design.widget.TextInputLayout>
LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<Button
android:id="@+id/submit"
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LinearLayout>


LinearLayout>
android.widget.LinearLayout>


activity_second.xml :


xml version="1.0" encoding="utf-8"?>
<android.widget.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"
tools:context="com.ourcoaching.simpleapplication.SecondActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Display Screen"
android:textAlignment="center"
android:textSize="30dp"/>
LinearLayout
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/name_display"
android:text="name"
android:textAlignment="center"/>
LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/email_display"
android:text="Email"
android:textAlignment="center"/>
LinearLayout>



LinearLayout>
android.widget.LinearLayout>


MainActivity.java:

package com.ourcoaching.simpleapplication;

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

public class MainActivity extends AppCompatActivity {

private EditText User_name,Email_ID;
private Button Submit_Button;

public static final String key_email="email",key_name="name";

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

// Instantiating the variables
User_name=(EditText)findViewById(R.id.name);
Email_ID=(EditText)findViewById(R.id.emailID);
Submit_Button=(Button)findViewById(R.id.submit);
}

@Override
protected void onResume() {
super.onResume();

Submit_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startNextActivityWithData();
}
});
}

private void startNextActivityWithData() {
String name,email;
name=User_name.getText().toString();
email=Email_ID.getText().toString();

if (name!=null && email!=null){

Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra(key_name,name);
intent.putExtra(key_email,email);

startActivity(intent);
}
}
}


SecondActivity.java

package com.ourcoaching.simpleapplication;

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

public class SecondActivity extends AppCompatActivity {

private TextView Name_display,Email_display;

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

// Instantiating the variables
Name_display=(TextView)findViewById(R.id.name_display);
Email_display=(TextView)findViewById(R.id.email_display);

Intent intent=getIntent();

String name_get,email_get;

name_get=intent.getStringExtra(MainActivity.key_name);
email_get=intent.getStringExtra(MainActivity.key_email);

if (name_get!=null){
Name_display.setText(name_get);
}

if (email_get!=null){
Email_display.setText(email_get);
}

}
}




Download the source code from GitHub



Related Tutorials