Giter Club home page Giter Club logo

compose-navigation's Introduction

Compose Navigation

API

This library will make it easier to pass arguments between screens in Jetpack Compose

Setup

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

dependencies {
	implementation 'com.github.doctor-blue:compose-navigation:1.1.0'
}

How to use?

Step 1: Create your screen class and extend it with ComposeScreen class

  • Example: I have 3 screens (MainScreen, Screen1, Screen2). As you can see the _route variable is my screen name
mport com.devcomentry.lib.ComposeScreen

sealed class Screen(_route: String) : ComposeScreen(_route) {
    object MainScreen : Screen("main_screen")
    object Screen1 : Screen("screen1")
    object Screen2 : Screen("screen2")
}

Step 2: Create a class and implement it with Argument interface. Variables in this class is your argument you wanna pass to other screen.

  • Example: I want to pass a number from MainScreen to Screen2. More variables means more arguments
package com.devcomentry.composenavigation.ui.screen

import com.devcomentry.lib.Argument

data class Screen2Argument(val number: Int = -1) : Argument

Step 3: Setup your navigation

  • Same as using navigation component, but at the composable function you can give ComposeScreen and Argument object, let me help you with the rest.
  • Support get argument from Bundle or SavedStateHandle. If Bundle or SavedStateHandle object doen't contains key argument will be default of Argument object
package com.devcomentry.composenavigation

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.devcomentry.composenavigation.ui.screen.*
import com.devcomentry.lib.composable
import com.devcomentry.lib.from

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

        setContent {
            Surface {
                Surface(
                    color = MaterialTheme.colors.background,
                ) {
                    val navController = rememberNavController()
                    NavHost(
                        navController = navController,
                        startDestination = Screen.MainScreen.route
                    ) {

                        composable(Screen.MainScreen) {
                            MainScreen(navController = navController)
                        }

                        composable(Screen.Screen1) {
                            Screen1()
                        }

                        composable(Screen.Screen2, Screen2Argument()) {
                            // get data from arguments
                            it.arguments?.let {  bundle->
                                val argument = Screen2Argument().from(bundle)
                                Screen2(argument)
                            }
                        }

                    }
                }
            }
        }
    }
}

Step 4: Navigate to other screen

  • Example 1: just open Screen1
    navController.navigate(Screen.Screen1.route)
  • Example 2: pass random number to Screen2
    navController.navigate(
                        Screen.Screen2.setParam(
                            Screen2Argument(
                                Random.nextInt(
                                    0,
                                    50
                                )
                            )
                        )
                    )

Note

  • You can get Argument object from arguments (Instance of Bundle) like example above.
    val argument = Screen2Argument().from(bundle)
    // use your argument
  • Or get data from SavedStateHandle like this
    val argument = Screen2Argument().from(savedStateHandle)
    // use your argument

Want more an example?

Donate

Buy Me A Coffee paypal

compose-navigation's People

Contributors

doctor-blue avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

compose-navigation's Issues

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.