/test

test

Primary LanguageKotlinApache License 2.0Apache-2.0

ActivityStarter

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

codebeat badge Build Status Stories in Ready

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 agruments.
  • Support flags and Intent provide.

Full documentation is located here. Here is TOC:

Example

This is Activity with starter made in standard way: (it is just short. See full example.)

public class MainActivity extends BaseActivity {

    private static String NAME_KEY = "nameArg";
    private static String ID_KEY = "idArg";
    private static String GRADE_KEY = "gradeArg";
    private static String PASSING_KEY = "passingArg";

    public static void start(Context context, String name, int id, char grade, boolean passing) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(NAME_KEY, name);
        intent.putExtra(ID_KEY, id);
        intent.putExtra(GRADE_KEY, grade);
        intent.putExtra(PASSING_KEY, passing);
        context.startActivity(intent);
    }

    String name;
    int id;
    char grade;
    boolean passing;

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

        Intent intent = getIntent();
        name = intent.getStringExtra(NAME_KEY);
        id = intent.getIntExtra(ID_KEY, -1);
        grade = intent.getCharExtra(GRADE_KEY, 'a');
        passing = intent.getBooleanExtra(PASSING_KEY, false);
    }
}

With ActivityStarter all you need is:

public class MainActivity extends BaseActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityStarter.fill(this); // This can be located in BaseActivity, one for all activities

        //...
    }

    @Override // This is optional, only when we want to keep arguments changes in case of rotation etc.
    protected void onSaveInstanceState(Bundle outState) { // Also can be located in BaseActivity, one for all activities
        super.onSaveInstanceState(outState);
        ActivityStarter.save(this, outState);
    }
}

And you start it nearly the same way:

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

Simillar 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);

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

Installation

For Java project add in build.gradle file:

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

For Kotlin project add in build.gradle file:

kapt {
    generateStubs = true
}

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

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.

Converters

If you are using Parceler library and you want to pass this objects as an arguments then you can do it using ActivityStarter:

@Arg StudentParceler student;

Instruction how to allow it are here. If you want to define your own converters then read about it 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.