Code Monkey home page Code Monkey logo

composeforlegacyprojects's Introduction

Legacy Project with Jetpack Compose Integration

This project is an experiment to integrate Jetpack Compose into a legacy Android application. The main goal is to use a generic Activity or Fragment as the entry point within a Compose module, utilizing the latest navigation-compose library to enable navigation at any point in the legacy application.

Purpose

The primary objective of this project is to demonstrate how to:

  1. Integrate Jetpack Compose in a legacy Android project.
  2. Use navigation-compose to handle navigation with serializable classes.
  3. Navigate through various parts of the legacy application using ComposeEntryActivity and ComposeEntryFragment.

Example Usage

Using ComposeEntryActivity

private fun setListeners() {
    with(binding) {
        btFragment.setOnClickListener {
            startActivity(FragmentActivity.create(this@MainActivity))
        }
        btScreenA.setOnClickListener {
            startComposeEntryActivity(FeatureARoutes.ScreenA("Activity"))
        }
        btScreenB.setOnClickListener {
            startComposeEntryActivity(FeatureARoutes.ScreenB)
        }
        btScreenC.setOnClickListener {
            startComposeEntryActivity(FeatureARoutes.ScreenC)
        }
        btScreenD.setOnClickListener {
            startComposeEntryActivity(FeatureBRoutes.ScreenD)
        }
        btScreenE.setOnClickListener {
            startComposeEntryActivity(FeatureBRoutes.ScreenE)
        }
        btScreenF.setOnClickListener {
            startComposeEntryActivity(FeatureBRoutes.ScreenF)
        }
    }
}

private fun startComposeEntryActivity(route: NavRoute) {
    startActivity(ComposeEntryActivity.create(this, route))
}

Using ComposeEntryFragment

private fun setListeners() {
    binding.btScreenA.setOnClickListener {
        navigate(FeatureARoutes.ScreenA("Fragment"))
    }
    binding.btScreenB.setOnClickListener {
        navigate(FeatureARoutes.ScreenB)
    }
    binding.btScreenC.setOnClickListener {
        navigate(FeatureARoutes.ScreenC)
    }
    binding.btScreenD.setOnClickListener {
        navigate(FeatureBRoutes.ScreenD)
    }
    binding.btScreenE.setOnClickListener {
        navigate(FeatureBRoutes.ScreenE)
    }
    binding.btScreenF.setOnClickListener {
        navigate(FeatureBRoutes.ScreenF)
    }
}

private fun navigate(destination: NavRoute) {
    ComposeEntryFragment.setRoute(destination)
    findNavController().navigate(R.id.action_FirstFragment_to_composeEntryFragment)
}

Once the Activity or Fragment is initiated, navigation continues using Compose and the navigation-compose library.

Project Structure

project
|- app
|- core
|   |- common
|   |- ui
|- compose
|- navigation
|- featureA
|   |- navigation
|   |- routes
|   |- presentation
|- featureB
    |- navigation
    |- routes
    |- presentation

Conclusion

This project demonstrates a great integration of Jetpack Compose into a legacy Android application. By leveraging navigation-compose and generic entry points such as Activity and Fragment, it enables flexible navigation throughout the app, providing a robust foundation for modernizing legacy codebases.

composeforlegacyprojects's People

Contributors

jujodevs avatar

Watchers

 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.