Code Monkey home page Code Monkey logo

compose-launcher's Introduction

Jetpack Compose Launcher

Goodbye Activity

When you're develop an Android application only with Jetpack Compose, you probably don't need to aware about Activities.

This library automatically create the Activity class from a Composable functions with an Entry annotation with KSP.

You can start development quickly without touching the Activity or the AndroidManifest.

Getting Started

1. Create new project without Activity

Select No Activity from Android Studio > File > New > New Project.

android studio wizard

2. Add dependencies

Add Maven Central repository to your build.gradle.

repositories {
    mavenCentral()
}

Add the package dependencies to your build.gradle.

You'll need to enable KSP.

build.gradle.kts
plugins {
    id("com.google.devtools.ksp").version("1.7.10-1.0.6")
}

android {
    // Make IDE aware of generated code
    sourceSets {
        getByName("debug") {
            kotlin.srcDirs("build/generated/ksp/debug/kotlin")
        }
        getByName("release") {
            kotlin.srcDirs("build/generated/ksp/release/kotlin")
        }
    }
}

dependencies {
    implementation("com.moriatsushi.launcher:launcher:1.0.0-alpha02")
    ksp("com.moriatsushi.launcher:launcher-processor:1.0.0-alpha02")
}

Add Jetpack Compose dependencies according to your needs.

build.gradle.kts
android {
    // ...

    buildFeatures {
        compose = true
    }

    compileOptions {
        sourceCompatibility(JavaVersion.VERSION_1_8)
        targetCompatibility(JavaVersion.VERSION_1_8)
    }

    composeOptions {
        kotlinCompilerExtensionVersion = "1.3.0"
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    // ...

    implementation("androidx.compose.ui:ui:1.2.1")
    implementation("androidx.compose.ui:ui-tooling:1.2.1")
    implementation("androidx.compose.foundation:foundation:1.2.1")
    implementation("androidx.compose.material:material:1.2.1")
}

3. Write Composable function with Entry annotation

By set default parameter to true, the screen will be displayed automatically when the application starts.

import androidx.compose.material.*
import androidx.compose.runtime.Composable
import com.moriatsushi.launcher.Entry

// A entry displayed when the application starts.
// The default entry is at most one.
@Entry(default = true)
@Composable
fun Main() {
    MaterialTheme {
        Scaffold {
            /* ... */
        }
    }
}

Launch Other Entry

It is possible to create multiple entries and transition between them.

import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import com.moriatsushi.launcher.Entry

@Entry(default = true)
@Composable
fun Main() {
    MaterialTheme {
        // `rememberOtherLauncher` is generated
        val otherLauncher = rememberOtherLauncher()

        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center,
        ) {
            Button(
                onClick = {
                    // Go to Other entry
                    otherLauncher.launch()
                },
            ) {
                Text(text = "launch Other page")
            }
        }
    }
}

@Entry
@Composable
fun Other() {
    MaterialTheme {
        /* ... */
    }
}

You can also transition from Activity or Fragment.

class SampleActivity : ComponentActivity() {
    // `getOtherLauncher` is generated
    private val launcher = getOtherLauncher(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.layout_file)
        findViewById<TextView>(R.id.text).setOnClickListener {
            // Go to Other entry
            launcher.launch()
        }
    }
}

compose-launcher's People

Contributors

mori-atsushi avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.