IntentLife框架帮助您轻松愉快地接收Intent数据

一般情况下,我们在一个Activity中接收上一个Activity传递的Intent携带的数据会写一堆getXXXExtra方法,需要传入key值,还要写强转代码,想想都觉得恶心…

于是本文推出IntentLife来帮助大家减少繁重的开发工作

IntentLife框架地址:https://github.com/ausboyue/IntentLife

一个自动绑定Intent携带的数据的android库。(An android library that automatically binds data carried by the Intent.)

如何使用?

一、引用框架

1. 在项目根目录下的build.gradle添加仓库地址:

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

2. 在需要使用框架的Module下的build.gradle添加依赖:

    dependencies {
          implementation 'com.github.ausboyue.IntentLife:intentlife:v1.0.0'
          annotationProcessor 'com.github.ausboyue.IntentLife:intentlife_compiler:v1.0.0'
    }

二、开始使用

1. ActivityA跳转ActivityB

  • ActivityA跳转代码可能如下:
        User user = new User();
        user.setUserId("9527");
        user.setName("Cheny");
        user.setJob("android developer");

        Intent intent = new Intent(activityA, ActivityB.class);
        intent.putExtra("key_user", user);
        startActivity(intent);
  • ActivityB中在需要绑定数据的属性上加IntentLife注解(@BindIntentKey),如:
    @BindIntentKey("key_user")
    User mUser;
  • 在ActivityB的onCreate方法中适当位置绑定当前Activity,完整代码如下:
    public class ActivityB extends AppCompatActivity {
        @BindIntentKey("key_user")
        User mUser;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_secend);
            //  IntentLife inject
            IntentLife.bind(this);
            
            TextView tv_user_name = findViewById(R.id.tv_user_name);
            tv_user_name.setText(
                    "Hello , I am " + mUser.getName()
                            + ".\nMy job is " + mUser.getJob() + ".");
        }
    }

这样就可以直接对mUser进行操作了。

框架支持

数据类型

  • 支持java八大基本数据类型及其数组和集合
  • 支持实现java序列化Serializable接口的类
  • 支持实现android序列化Parcelable接口的类及其数组和集合
  • 下个版本支持Bundle数据

界面场景

  • 支持Activity间的跳转
  • 下个版本支持加载Fragment

使用注意

需要的绑定的属性不应有private修饰,否则无法正常绑定数据。

猜你喜欢

转载自blog.csdn.net/ausboyue/article/details/92582829
今日推荐