Category

Activity

Category

How To Move From One Activity To Another In Android

how to move from one activity to another in android
Do you want to know how to move from one Activity to Another in Android?

Okay…Let’s go.



How to start new Activity using Intent in Android

In Android, we can use Intent to start another activity. Intent can be used to launch other app activities too. But now we simply describe starting a new activity.



val intent = Intent(this@MainActivity, SecondActivity::class.java)
startActivity(intent)


this@MainActivity(MainActivity.this) – Here means Context, Activity is a subclass of Context.

That’s why we use this@MainActivity here. You can also use this, but assure that it’s a context.

SecondActivity::class.java(SecondActivity.this) – Name of Activity which you want to start.

Let’s dig more…

Two main parts here.

  • Intent(Context packageContext, Class cls) – Using this Intent’s constructor, we can create a way to our second activity.
  • startActivity(Intent intent) – It’s one of the methods in Activity class and launches activity specified in the Intent. If there is no Activity found, ActivityNotFoundException() gets called.

Intent needs a Java reference of class. That’s why We use here class.java part in Kotlin.

Create an Android Studio project

In this example, We just create two activities, when you click on the button, It shows Second Activity. Here I have used OnClickListener for click events, You can also use android:onClick”attribute.

Okay.

Open Android Studio and start a new Android Studio project.

Application name : Start New Activity Ex.

Company domain: androidride.com

Check to include Kotlin support.


Select minimum SDK:API 15 – Android 4.0.3 (Ice Cream Sandwich) and click Next.

Next dialog, Select Empty Activity and click Next.

Activity Name: MainActivity
Check Generate layout file

Layout Name: activity_main

Click Finish.

build.gradle(Project: StartNewActivityEx – Kotlin)



// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()    
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


build.gradle(Module:app)



apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.androidride.startnewactivityex"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}


colors.xml



<!--?xml version="1.0" encoding="utf-8"?-->
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>


strings.xml



<resources>
    <string name="app_name">Start New Activity Ex</string>
</resources>


styles.xml



<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>


activity_main.xml



<!--?xml version="1.0" encoding="utf-8"?-->
 	<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center">

    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="First Activity" android:textstyle="bold" android:textsize="45sp" android:layout_marginbottom="25dp">

    </textview></linearlayout>

MainActivity.kt



package com.androidride.startnewactivityex

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    button.setOnClickListener{

        val intent = Intent(this@MainActivity,SecondActivity::class.java)
        startActivity(intent)

        }
    }
}



Create Second Activity

Right-click on App-> New -> Activity -> Empty Activity ->

Activity Name: SecondActivity

check Generate Layout File.

Layout Name: activity_second

Select your source language Kotlin/Java.

and click Finish.

activity_second.xml



<!--?xml version="1.0" encoding="utf-8"?-->
 	<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center">

    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textstyle="bold" android:textsize="45sp" android:text="Second Activity">

</textview></linearlayout>

SecondActivity.kt



package com.androidride.startnewactivityex

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class SecondActivity : AppCompatActivity()
{

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)


    }
}

Run now.

launch new activity in android

This is so simple. Isn’t it?

If you found this article helpful. please share it.

Reference:

startActivity()

Intent class – google docs

Please disable your adblocker or whitelist this site!