CachePot is a simple open source for data cache management and passing data object between Fragments(Activities) without Pacelable and Serializable.
WARNING: It would be inappropriate to pass data both ways between different android applications, it's a better way to use
Intent
properly.
Gradle:
repositories {
jcenter()
}
dependencies {
compile 'com.github.kimkevin:cachepot:1.2.0’
}
Maven:
<dependency>
<groupId>com.github.kimkevin</groupId>
<artifactId>cachepot</artifactId>
<version>1.2.0</version>
</dependency>
It works anywhere. there're examples as below
- Between
Activity
andActivity
- Between
Activity
andFragment
- Between
Fragment
andFragment
- From
PagerAdapter
to individualFragment
Push your data object to CachePot
instance in your Activity
or Fragment
.
public void push(Object data)
KoreanFood foodItem = new KoreanFood(1, "Kimchi", "Traditional fermented Korean side dish made of vegetables")
CachePot.getInstance().push(foodItem);
// open your activity or fragment
Pop your data object from CachePot
after view changes
public Object pop(Class classType)
public class MainFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
KoreanFood foodItem = CachePot.getInstance().pop(KoreanFood.class);
}
Push
List<KoreanFood> foodItems = new ArrayList<>();
foodItems.add(new KoreanFood(1, "Kimchi", "Traditional fermented Korean side dish made of vegetables"));
foodItems.add(new KoreanFood(2, "Kkakdugi", "A variety of kimchi in Korean cuisine"));
CachePot.getInstance().push(foodItems);
// open your activity or fragment
Pop
List<KoreanFood> foodItems = CachePot.getInstance().pop(ArrayList.class);
How to pass Object(Model) Asynchronously when using ViewPager
First push your data object with position
or id
to CachePot
instance in FragmentStatePagerAdapter
(or FragmentPagerAdapter
)
public void push(long id, Object data)
private class PagerAdapter extends FragmentStatePagerAdapter {
...
public Fragment getItem(int position) {
KoreanFood foodItem = foodItems.get(position);
CachePot.getInstance().push(position, foodItem);
// or
CachePot.getInstance().push(foodItem.getId(), foodItem);
...
}
}
public Object pop(long id)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (getArguments() != null) {
final int position = getArguments().getInt(ARG_POSITION);
foodItem = CachePot.getInstance().pop(position);
// or
foodItem = CachePot.getInstance().pop(id);
}
}
If more complecated views, use TAG
public void push(String tag, Object data)
public void push(String tag, long id, Object data)
private class PagerAdapter extends FragmentStatePagerAdapter {
...
public Fragment getItem(int position) {
KoreanFood foodItem = foodItems.get(position);
CachePot.getInstance().push(TAG, position, foodItem);
// or
CachePot.getInstance().push(TAG, foodItem.getId(), foodItem);
...
}
}
public Object pop(String tag)
public Object pop(String tag, long id)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (getArguments() != null) {
final int position = getArguments().getInt(ARG_POSITION);
foodItem = CachePot.getInstance().pop(TAG, position);
// or
foodItem = CachePot.getInstance().pop(TAG, id);
}
}
All contributions are welcome. Open a Pull Requests or refer to the Issues section.
MIT
Copyright (c) 2016-present, “KimKevin” Yongjun Kim