Code Monkey home page Code Monkey logo

jetpack-navigation's Introduction

JetPack-Navigation

Android JetPack is a set of android components designed with Kotlin in mind, available with Android Studio 3.2.
This library provode major 4 component

Navigation
DataMnager
Slice
Paging

Navigation_graph


<fragment
    android:id="@+id/loginFragment"
    android:name="com.samset.jetpack.fragment.LoginFragment"
    android:label="fragment_login"
    tools:layout="@layout/fragment_login" >

    <argument
        android:name="title"
        android:defaultValue="@string/signupdata" />

    <action
        android:id="@+id/action_loginFragment_to_signUpFragment"
        app:destination="@id/signUpFragment" />
</fragment>

<fragment
    android:id="@+id/signUpFragment"
    android:name="com.samset.jetpack.fragment.SignUpFragment"
    android:label="fragment_sign_up"
    tools:layout="@layout/fragment_sign_up" >

    <argument
        android:name="signupdata"
        android:defaultValue='""' />
</fragment>

Java code set your fragment like.


getSupportFragmentManager().replaceFragment(R.id.maincontainer,new YourFragment()).commit;

Kotlin with JetPack set fragment


    val navController = findNavController(mainNavigationFragment)
    NavigationUI.setupActionBarWithNavController(this, navController)
    navController.navigate(R.id.loginFragment)

JetPack set fragment and pass bundle data


        val action = LoginFragmentDirections.action_loginFragment_to_signUpFragment()
        action.setSignupdata("Hello sam! How are you?")
        
        val navController = view.findNavController()
        navController.navigate(action)

JetPack set fragment and pass custom bundle data


        infos.name="samset"
        infos.mobileNo="9811054xxx"

        val bundle = Bundle()
        bundle.putSerializable("signupdata",infos)
        
        val navController = view.findNavController()
        navController.navigate(R.id.signUpFragment,bundle)

Developers Importent Point


If you want Maintain fragment backstack then override this method in your MainActivity

override fun onSupportNavigateUp()=findNavController(R.id.mainNavigationFragment).navigateUp()

If you want not maintain the backstack specific fragment then you add one line of code in navigation_graph like..

 <fragment
     android:id="@+id/fragmentSecvond"
     android:name="com.samset.jetpacknavigation.fragments.FragmentSecond"
     android:label="fragment_fragment_secvond"
     tools:layout="@layout/fragment_fragment_secvond">
        <argument
         android:name="mydata"
         android:defaultValue="defaulttext"
         app:type="string" />
        <action
         android:id="@+id/action_fragmentSecvond_to_fragmentThird"
         app:clearTask="true"   //** add this line for clear backstack **
         app:destination="@id/fragmentThird" />
  </fragment>
  

You can also be change the action "Pop To" behavior from attribute pannel( from xml app:popUpTo="@+id/FragmentOne). This way when user will click back, he will be navigated to FragmentOne instead of the original destination.

 <fragment
   android:id="@+id/fragmentThird"
   android:name="com.samset.jetpacknavigation.fragments.FragmentThird"
   android:label="fragment_fragment_third"
   tools:layout="@layout/fragment_fragment_third">
     <action
      android:id="@+id/action_fragmentThird_to_fragmentFourth"
      app:destination="@id/fragmentFourth" />
     <action
      app:popUpTo="@id/action_fragmentSecond" // <b>.add this line and change pop to destination</b>
      android:id="@+id/action_fragmentThird_to_fragmentOne"
      app:destination="@id/fragmentOne" />
</fragment>

more details Blog Pages.

jetpack-navigation's People

Contributors

samsetdev avatar

Stargazers

 avatar

Watchers

 avatar  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.