Back and Forth Between Two Activities
Android Sample Codes

Back and Forth Between Two Activities

Back and forth between two activities in android is very simple in android programming. Make sure you have the Android Studio installed in your system.If you are a beginner in Android development, the first thing you need to do is to create two screens and try to switch from one screen to another. In this tutorial, you will learn how to move from one screen to another or back and forth between two activities in android.

The basic component of an Android application is Activity. This can be considered as the screen visible to the user. Now let’s create two activities and switch from one activity to another when a button is pressed.

Back and Forth Between Two Activities

Start a new project in Android Studio using an Empty Activity. Here I just given my project name is “from one anroid activity to another activity“.Then you can see MainActivity.java file and one activity_main.xml file as shown below.

Back and Forth Between Two Activities
                                     App Home Page

Modify both files (MainActivity.java, activity_main.xml ) as shown below.

MainActivity.java 

package com.example.mca.fromoneanroidactivitytoanotheractivity;

import android.content.Context;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        final Context context = this;

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View arg0) {

                Intent intent = new Intent(context, SecondActivity.class);

                startActivity(intent);

            }

        });

    }

}

activity_main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="200dp" android:layout_width="match_parent" android:orientation="vertical">

<TextView

android:id="@+id/textView1"

android:textAlignment="center"

android:layout_width="match_parent"

android:textColor="#800000"

android:textStyle="bold"

android:layout_height="100px"

android:text="You are in First Activity"

/>

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textColor="#800000"

        android:textAlignment="center"

        android:textStyle="bold"

        android:text="Click Here To Go To Second Activity" />

</LinearLayout>

Let’s add another activity by right-click on app in the left-hand panel and select New → Activity → Empty Activity.

 

Back and Forth Between Two Activities
                             Creation of Second Activity and Layout

Give it a name — here I just named activity name is  “SecondActivity” and specify a layout name also , activity_second.

Click on Finish and you should see two new files created — a new Java file, SecondActivity.java and new layout XML file, activity_second.xml.

Back and Forth Between Two Activities
                           Files view in “Back and Forth Between Two Activities ” App

To allow the user to back and forth between the MainActivity to the SecondActivity that we have created already, add a Button and a method that will be ran when that button is tapped.

SecondActivity.java

package com.example.mca.fromoneanroidactivitytoanotheractivity;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

class SecondActivity extends MainActivity {

    Button button;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_second);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        final Context context = this;

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View arg0) {

                Intent intent = new Intent(context, MainActivity.class);

                startActivity(intent);

            }

        });

    }

}

 activity_second.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="200dp" android:layout_width="match_parent" android:orientation="vertical">

    <TextView

        android:id="@+id/textView1"

        android:layout_width="match_parent"

        android:layout_height="100px"

        android:text="You are in Second Activity"

        android:textAlignment="center"

        android:textColor="#800000"

        android:textStyle="bold" />

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textColor="#800000"

        android:textAlignment="center"

        android:textStyle="bold"

        android:text="Click Here To Go To First Activity" />

</LinearLayout>

Also modify the AndroidManifest.xml file as shown below. Whenever you create a new activity you should add that activity in the manifest file.

AndroidManifest.xml

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

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

    package="com.example.mca.fromoneanroidactivitytoanotheractivity">

    <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>

 Output :

When you run the app, the activity screen looks like

Back and Forth Between Two Activities

Back and Forth Between Two Activities
                Second Activity

Tap on the button named “CLICK HERE TO GO TO SECOND ACTIVITY”, it will direct you to the another activity screen ( SecondActivity.java) as show below.

 

Tap on the button named “CLICK HERE TO GO TO FIRST ACTIVITY” to go to the first activity screen that we shown already when ran the app first time. You can move from one activity to another activity by tapping on the button.

4 Replies to “Back and Forth Between Two Activities

Leave a Reply

Your email address will not be published. Required fields are marked *