DataAutoAccess
自动存取Android Bundle中数据——给需要自动存取的变量添加注解,编译时会通过注解处理自动生成存取的代码
Usages
- Activity启动时自动取出Intent中的数据,并赋值给相应的field
- Activity由于系统内存不足将要被杀死时,在onSaveInstanceState方法里存储数据,Activity重启时在onCreate中取出数据并赋值给相应的field
Use
public class ExampleActivity extends Activity{
@AutoAccess String name;
@AutoAccess String description;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get data
Bundle data;
data = getIntent().getExtras();
//judge data source
if (data == null) {
data = savedInstanceState;
}
DataAutoAccess.getData(this, data);
//TODO use fields...
}
@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//save data
DataAutoAccess.saveData(this, outState);
}
}
Intent传参形式:
Intent intent = new Intent(this, TestActivity.class);
intent.putExtra("name", "DataAutoAccess");
intent.putExtra("description", "Android bundle data auto access.");
startActivity(intent);
经过以上配置之后,DataAutoAccess会自动从intent中取出数据,给name和description变量赋值,而且当activity由于系统内存不足被杀死时,也会自动保存变量值,在onCreate时取出进行自动赋值。是不是感觉到挺Cool!
Code
欢迎大家评论,喜欢的就去github上star吧 O(∩_∩)O