GithubHelp home page GithubHelp logo

aptintent's Introduction

AptIntent

Build Status

AptIntent是一个Android平台基于annotation的库,其目的是简化创建带数据的Intent和获取Intent内的数据。

使用方法

1.添加依赖

Gradle

在你的工程目录下的build.gradle添加android-apt插件,添加jitpackmaven库:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
    	...
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
	repositories {
		...
		maven { url "https://www.jitpack.io" }
	}
}

然后在你的模块下的build.gradle应用android-apt插件,添加AptIntent依赖:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
	compile 'com.github.zhl3391.AptIntent:aptintent:1.0.1'
	apt 'com.github.zhl3391.AptIntent:aptintent-compiler:1.0.1'
}

2.定义接口

public interface IntentCreator {

    String KEY_STRING       = "string";
    String KEY_INT          = "int";
    String KEY_BOOLEAN      = "boolean";
    String KEY_OBJECT       = "object";
    String KEY_ARRAY_LIST   = "array_list";
    String KEY_BUNDLE       = "bundle";

    @CreateIntent(MainActivity.class)
    Intent mainActivityIntent(Context context,
                              Bundle bundle,
                              @Extra(KEY_STRING)String stringTest,
                              @Extra(KEY_INT) int intTest,
                              @Extra(KEY_BOOLEAN) boolean booleanTest,
                              @Extra(KEY_OBJECT) ObjectTest objectTest,
                              @Extra(KEY_ARRAY_LIST) ArrayList<String> arrayListTest);
}

定义一个IntentCreator接口,定义一个mainActivityIntent的方法,通过@CreateIntent注解声明, 通过@Extra注解声明参数。
注:必须传入Context

3.获取参数和跳转

public class MainActivity extends AppCompatActivity {

    @ExtraField(KEY_STRING)
    String mStringTest;
    @ExtraField(KEY_INT)
    int mIntTest;
    @ExtraField(KEY_BOOLEAN)
    boolean mBooleanTest;
    @ExtraField(KEY_OBJECT)
    ObjectTest mObjectTest;
    @ExtraField(KEY_ARRAY_LIST)
    ArrayList<String> mArrayListTest;
    @ExtraField(KEY_BUNDLE)
    String mBundleTest;

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

        AptIntent.bind(this);
    }

    public void jump(View view) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("arrayList");
        Bundle bundle = new Bundle();
        bundle.putString(KEY_BUNDLE, "bundle");
        Intent intent = AptIntent.create(IntentCreator.class).mainActivityIntent(this,
                bundle, "haha", 888, true, new ObjectTest(), arrayList);
        startActivity(intent);
    }
}

通过@ExtraField注解声明成员变量,调用AptIntent.bind(this)获取intent内数据。
调用AptIntent.create(IntentCreator.class)创建IntentCreator接口的实现,再调用mianActivityIntent创建intent
注:@ExtraField注解的成员变量不能用privatestatic修饰。

License

Copyright 2016 zhl3391

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.

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.