Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork11
🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.
License
skydoves/Bundler
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.
Add below codes to yourrootbuild.gradle
file (not your module build.gradle file).
allprojects { repositories { mavenCentral() }}
And add a dependency code to yourmodule'sbuild.gradle
file.
dependencies { implementation"com.github.skydoves:bundler:1.0.4"}
Snapshots of the current development version of Bundler are available, which trackthe latest versions.
repositories { maven { url'https://oss.sonatype.org/content/repositories/snapshots/' }}
intentOf
is an expression for creating an Intent using Kotlin DSL style and we can put extras using theputExtra
method. Also, we can put extras using the+
keyword in front of a key/value pair.
val intent= intentOf { putExtra("posterId", poster.id)// put a Long type 'posterId' value. putExtra("posterName" to poster.name)// put a String type 'posterName' value. putExtra("poster", poster)// put a Parcelable type 'poster' value.+("id" to userInfo.id)// put a Long type 'id' value.+("name" to userInfo.nickname)// put a String type 'name' value.-"name"// remove a String type 'name' value.}
We can start activities using theintentOf
expression like below.
intentOf<SecondActivity> { putExtra("id" to userInfo.id) putExtra("name" to userInfo.nickname) putExtra("poster", poster) startActivity(this@MainActivity)}
We can also use other options for creating an intent.
intentOf { setAction(Intent.ACTION_MAIN) addCategory(Intent.CATEGORY_APP_MUSIC) setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(this@MainActivity)}
bundle
is an expression for initializing lazily extra values from the intent.
classSecondActivity :AppCompatActivity() {privateval id:Long by bundle("id",-1)// initializes a Long extra value lazily.privateval name:String by bundle("name","")// initializes a String extra value lazily.privateval poster:Poster? by bundle("poster")// initializes a Parcelable extra value lazily.// -- stubs -- //
We can initialize a Parcelable value with a defaut value.
privateval poster:Poster? by bundle("poster") {Poster.create() }
Also, we can initialize type of Array and ArrayList usingbundleArray
andbundleArrayList
expression.
// initialize lazily without default values.privateval posterArray by bundleArray<Poster>("posterArray")privateval posterListArray by bundleArrayList<Poster>("posterArrayList")or// initialize lazily with default values.privateval posterArray by bundleArray<Poster>("posterArray") { arrayOf() }privateval posterListArray by bundleArrayList<Poster>("posterArrayList") {arrayListOf() }
The below example shows setting arguments using theintentOf
expression.
arguments= intentOf {+("id" to userInfo.id)+("name" to userInfo.nickname)+("poster" to poster)}.extras
We can initialize argument values lazily in Fragments using thebundle
expression like below.
- val id: Long = arguments?.getLong("id", -1) ?: -1+ val id: Long by bundle("id", -1)- val poster: Poster? = arguments?.getParcelable("poster")+ val poster: Poster? by bundle("poster")
Thebundle
expression for initializing objects (e.g. Bundle, CharSequence, Parcelable, Serializable, Arrays), the property type must be null-able. But If we want to initialize them non-nullable type, we can initialize them to non-nullable type using thebundleNonNull
expression.
- val poster: Poster? by bundle("poster")+ val poster: Poster by bundleNotNull("poster")
We can observe the bundle data asLiveData
using theobserveBundle
expression. If there are no extra & arguments in the Activity or Fragment,null
will be passed to the observers. TheobserveBundle
emits data only a single time to a single observer. So We can observe only once using one observer. And the observer will be unregistered from the LiveData after observing data at once.
privateval id:LiveData<Long> by observeBundle("id",-1L)privateval poster:LiveData<Poster> by observeBundle("poster")id.observe(this) { vm.id= it}poster.observe(this) { binding.name= it.name}
We can also retrieve intent & arguments extra values from Activity and Fragment immediately. We can usebundleValue
,bundleNonNullValue
,bundleArrayValue
,bundleArrayListValue
.
val id= bundleValue("id",100L)val name= bundleValue("name","")val poster= bundleValue<Poster>("poster")
Support it by joiningstargazers for this repository. ⭐
Andfollow me for my next creations! 🤩
Copyright 2020 skydoves (Jaewoong Eum)Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
About
🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.