Start Another Activity In Same App

Start Another Activity In Same App

Now we are going to learn how to open another activity in the android application. In other languages, I can say how to navigate to another page in the android application.

This is very simple if you have created at least two activities in your app. You can start another activity within the same application by calling startActivity(). if the activity to be started as an action name defined within the AndroidManifest.xml.

You can send parameter with the intent when you open another activity.

Intent intent = new Intent(MainActivity.this, Second_Activity.class);
intent.putExtra("Action", "Sample Parameter");

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.legendblogs.demo">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.legendblogs.demo.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.legendblogs.demo.Second_Activity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize">
        </activity>
    </application>
</manifest>

activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.legendblogs.demo.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Linking with two pages"
        android:id="@+id/textView"
        android:layout_marginTop="91dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me to jump second page"
        android:id="@+id/button"
        android:layout_marginTop="48dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

second_page.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.legendblogs.demo.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Welcome to second Page"
        android:id="@+id/textView"
        android:layout_marginTop="91dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me to go first Page"
        android:id="@+id/button"
        android:layout_marginTop="48dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

MainActivity.java

package com.legendblogs.demo;
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;
/**
 * Created by amit on 21-09-2017.
 */
public class MainActivity extends AppCompatActivity {
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.context = this;
        Button button_toast = (Button) findViewById(R.id.button);
        button_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Second_Activity.class);
                intent.putExtra("Action", "Sample Parameter");
                startActivity(intent);
            }
        });
    }
}

SecondActivity.java

package com.legendblogs.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
 * Created by amit on 21-09-2017.
 */
public class Second_Activity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_page);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            Toast.makeText(this,"Intent Action: "+ extras.getString("Action"),Toast.LENGTH_LONG).show();
        }
        Button button_toast = (Button) findViewById(R.id.button);
        button_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Second_Activity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

Related posts

Write a comment