Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Simplifying Fragment newInstance() implementation #270

@overengineered

Description

@overengineered

I often create companion methods for Fragments that create new instance of the fragment and a helper to initialise arguments for that new instance is really helpful, e.g.:

class MyFragment : Fragment() {
    companion object {
        const val LABEL = "label"
        const val INDEX = "index"

        fun newInstance(label: String, value: Int): PostFragment {
            return MyFragment().withArguments {
                putString(LABEL, label)
                putInt(INDEX, index)
            }
        }
    }
 }

Possible implementation

inline fun <T: Fragment> T.withArguments(block: Bundle.() -> Unit): T
        = this.apply { arguments = Bundle().apply(block) }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions