Let us look at a Android ActivityLaunchMode example.

Example 1: ActivityLaunchModeDemo

This example will comprise the following files:

  • A.kt
  • B.kt
  • C.kt

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project

Step 2: Add Dependencies

In your app/build.gradle add dependencies as shown below:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "tw.hankli.activitylaunchmodedemo"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Step 3: Design Layouts

*(a). activity.xml

Create a file named activity.xml and design it as follows:

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

    <TextView
            android:id="@+id/view_txt"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <Button
            android:id="@+id/view_btn1"
            android:text="open A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <Button
            android:id="@+id/view_btn2"
            android:text="open B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <Button
            android:id="@+id/view_btn3"
            android:text="open C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

Step 4: Write Code

Write Code as follows:

*(a). A.kt

Create a file named A.kt

Here is the full code

package tw.hankli.activitylaunchmodedemo

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity.*

class A : AppCompatActivity() {

    companion object {
        fun start(context: Context) {
            val i = Intent(context, A::class.java)
            context.startActivity(i)
        }
    }

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

        // show the class name
        view_txt.text = this::class.java.simpleName

        // start A
        view_btn1.setOnClickListener { A.start(this) }

        // start B
        view_btn2.setOnClickListener { B.start(this) }

        // start C
        view_btn3.setOnClickListener { C.start(this) }
    }
}

*(b). B.kt

Create a file named B.kt

Here is the full code

package tw.hankli.activitylaunchmodedemo

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity.*

class B : AppCompatActivity() {

    companion object {
        fun start(context: Context) {
            val i = Intent(context, B::class.java)
            context.startActivity(i)
        }
    }

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

        // show the class name
        view_txt.text = this::class.java.simpleName

        // start A
        view_btn1.setOnClickListener { A.start(this) }

        // start B
        view_btn2.setOnClickListener { B.start(this) }

        // start C
        view_btn3.setOnClickListener { C.start(this) }
    }
}

*(c). C.kt

Create a file named C.kt

Here is the full code

package tw.hankli.activitylaunchmodedemo

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity.*

class C : AppCompatActivity() {

    companion object {
        fun start(context: Context) {
            val i = Intent(context, C::class.java)
            context.startActivity(i)
        }
    }

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

        // show the class name
        view_txt.text = this::class.java.simpleName

        // start A
        view_btn1.setOnClickListener { A.start(this) }

        // start B
        view_btn2.setOnClickListener { B.start(this) }

        // start C
        view_btn3.setOnClickListener { C.start(this) }
    }
}

Run

Simply copy the source code into your Android Project,Build and Run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Categorized in: