This Android library helps to manage static data more structurally by Kickback processor.
It let you persist data statically, get and free easily.
dependencies {
implementation'com.github.skydoves:kickback:1.0.3'annotationProcessor'com.github.skydoves:kickback-processor:1.0.3'
}Create an abstract class with @KickbackBox annotation.
@Kickback annotation does the role of an entity of static objects.
@KickbackBox(name = "Profile")
publicabstractclassUserProfile {
finalintvisits = 1;
@KickbackElement(name = "name")
finalStringuserName = "skydoves";
@Weak@KickbackElement(name = "password")
finalintuserPassword = 1234;
@Weak@KickbackElement(name = "secondPassword")
finalintuserSecondPassword = 12345;
@Soft@KickbackElement(name = "dog")
DoguserDog;
@SoftUserPrivatesuserPrivates;
}@KickbackElement annotation renames the object's name on your taste.
If not annotated, the default name of the element variable will be field name.
@Weak and @Soft annotation make an instance of a field to reference object.
And you can boxing not only primitive type but also POJO objects.
After builds your project, Kickback_(box name) class will be auto-generated by Kickback processor.
Then you can get instance of new generated class like below.
Kickback_Profilebox_profile = Kickback_Profile.getInstance();Then we can put, take out or free at memory into our new generated box class.
Kickback_Profilebox_profile = Kickback_Profile.getInstance();
box_profile.setPassword(4321); // persist "Password" element databox_profile.setDog(newDog("Akita", 4, "White")); // persist "Dog" object element databox_profile.getName(); // get value of "Name" elementbox_profile.freeVisits() // free "Visit" elementKickback_Profile.freeAll(); // free all elementsKickback_Profile.getBoxName(); // get box name@KickbackFunction annotation processes getter and setter functions.
@KickbackFunction's "keyname" value determines a target. The target should be a keyName.
Function's name should start with "set" or "get" prefix.
@KickbackFunction(keyname = "name")
publicStringsetNameFunction(StringuserName) {
returnuserName + "!!!";
}
@KickbackFunction(keyname = "name")
publicStringgetNameFunction(StringuserName) {
return"Hello" + userName;
}Sometimes we must pass extra data for using the data next Activity.
@KickbackBox(name = "SecondActivityExtra")
publicclassSecondActivityExtrasBox {
@KickbackElement(name = "name")
finalStringuserName = "skydoves";
@Weak@KickbackElement(name = "password")
finalintuserPassword = 0;
@KickbackFunction(keyname = "name")
publicStringsetNameFunction(StringuserName) {
returnuserName + "!!!";
}
@KickbackFunction(keyname = "name")
publicStringgetNameFunction(StringuserName) {
return"Hello" + userName;
}
}Do not need to put Extra data in bundle.
In this case, we do not have to implement Parcerable object and putExtra_ and getExtras.
Instead, Kickback persists data at the memory and you can free on the memory when you want.
publicclassMainActivityextendsAppCompatActivity {
Kickback_SecondActivityExtrabox = Kickback_SecondActivityExtra.getInstance();
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
box.setName("skydoves");
box.setPassword(1234);
Buttonbutton = findViewById(R.id.button0);
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(Viewview) {
startActivity(newIntent(this, SecondActivity.class));
}
});
}After using data at next Activity, you can remove data from memory onDestroy
publicclassSecondActivityextendsAppCompatActivity {
Kickback_SecondActivityExtrabox = Kickback_SecondActivityExtra.getInstance();
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toast.makeText(this, box.getName() + " :" + box.getPassword(), Toast.LENGTH_SHORT).show();
}
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
Kickback_SecondActivityExtra.freeAll(); // free all elements from memory
}
}In Android project, we can set LifecycleObserver and get LifecycleObserver from box class.
Using by these methods, we can more prevent our project from OOM.
kickback_profile.setLifecycleObserver(LifecycleOwnerlifecycleOwner)
kickback_profile.getLifecycleObserver()Copyright 2017 skydoves
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.