GithubHelp home page GithubHelp logo

ajrulez / activitystarter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcinmoskala/activitystarter

0.0 1.0 0.0 630 KB

Simple Android Library, that provides easy way to start the Activities with arguments.

Home Page: http://marcinmoskala.com/ActivityStarter/

License: Apache License 2.0

Java 6.36% Kotlin 93.40% Shell 0.25%

activitystarter's Introduction

ActivityStarter

Android Library that provide simpler way to start the Activities with multiple arguments.

codebeat badge Build Status Stories in Ready Join the chat at https://gitter.im/ActivityStarter/Lobby

Field and method binding for Android Activity arguments, which uses annotation processing to generate boilerplate code for you, and:

  • Eliminate all putExtra and getXXXExtra methods.
  • Allows you to forget about all keys that were used to pass arguments.
  • Support flags and Intent provide.

Full documentation is located here. Here is TOC:

Example

With ActivityStarter, to pass arguments to Activity, Fragment, Service or BroadcastReceiver, all you need is @Arg annotation before parameters that needs to be passed:

public class MainActivity extends BaseActivity {

    @Arg String name;
    @Arg int id;
    @Arg char grade;
    @Arg boolean passing;
}

And ActivityStarter.fill(this); in BaseActivity:

class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityStarter.fill(this);
    }

    @Override // This is optional, only when we want to keep arguments changes in case of rotation etc.
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ActivityStarter.save(this);
    }
}

Then, you can start Activity using generated starter:

MainActivityStarter.start(context, name, id, grade, passing);

Similar way, you can take Intent or start activity with flags:

MainActivityStarter.getIntent(context, name, id, grade, passing);
MainActivityStarter.startWithFlags(context, name, id, grade, passing, FLAG_ACTIVITY_SINGLE_TOP);

Arguments can be passed to Activities, Fragments, Services or BroadcastReceiver. Arguments can also be Optional.

Optional

You can make optional arguments:

public class MainActivity extends BaseActivity {

    @Arg(optional = true) String name;
    @Arg(optional = true) long id = -1;
}

Then will be generated also generators that does not contain them:

MainActivityStarter.start(context);
MainActivityStarter.start(context, name);
MainActivityStarter.start(context, id);
MainActivityStarter.start(context, name, id);

Further reading here.

Kotlin

ActivityStarter is supporting Kotlin to allow properties that are not-null and both read-write or read-only:

class StudentDataActivity : BaseActivity() {

    @get:Arg(optional = true) var name: String by argExtra(defaultName)
    @get:Arg(optional = true) val id: Int by argExtra(defaultId)
    @get:Arg var grade: Char  by argExtra()
    @get:Arg val passing: Boolean by argExtra()
}

Values are taken lazily and kept as fields, but there are still saved if ActivityStarter.save(this) is called in onSaveInstanceState. When all properties are provided by delegate, then there is no need to call ActivityStarter.fill(this) in onCreate.

Installation

For Java project add in build.gradle file:

dependencies {
    compile 'com.marcinmoskala.activitystarter:activitystarter:1.00'
    apt 'com.marcinmoskala.activitystarter:activitystarter-compiler:1.00'
}

For Kotlin project add in build.gradle file:

apply plugin: 'kotlin-kapt'

dependencies {
    compile 'com.marcinmoskala.activitystarter:activitystarter:1.00'
    kapt 'com.marcinmoskala.activitystarter:activitystarter-compiler:1.00'
}

If you want to use Kotlin-specific elements (property delegate argExtra), then add in build.gradle file:

apply plugin: 'kotlin-kapt'

dependencies {
    compile 'com.marcinmoskala.activitystarter:activitystarter:1.00'
    compile 'com.marcinmoskala.activitystarter:activitystarter-kotlin:1.00'
    kapt 'com.marcinmoskala.activitystarter:activitystarter-compiler:1.00'
}

And while library is located on JitPack, remember to add on module build.gradle (unless you already have it):

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

More information on Installation page.

Parceler

Since version 0.70, there is native support for Parceler library. To wrap and unwrap parameter using Parceler, use Arg annotation with parceler property set to true:

@Arg(parceler = true) StudentParcel studentParceler;

See example here.

License

Copyright 2017 Marcin Moskała

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

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations under the License.

activitystarter's People

Contributors

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