Questions CachePot helps answer.
Here is a summary of the problems cachepot helps you answer:
- How to Pass Data Between Activities
- How to Pass Data Between Fragments
- How to pass multiple data types between activities/fragments.
- How to Pass Data Between Activities and Fragments
- How to Pass Data From PageAdapter to Fragments.
Difference to other similar Libraries
- Its easy to use.
- Complete, encompassing both fragments and activities.
- Ability to pass single or multiple data or types. Easily pass custom objects or collections for example.
- No annotations or extension of classes.
CachePot - Android Simple Data Cache
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.
Download
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>
Usage
It works anywhere. there're examples as below
- Between
Activity
andActivity
- Between
Activity
andFragment
- Between
Fragment
andFragment
- From
PagerAdapter
to individualFragment
1. Single Type, Single Data
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);
}
In case of Collection or Map
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);
2. Single Type, Multiple Data
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);
}
}
Contributing
All contributions are welcome. Open a Pull Requests or refer to the Issues section.
License
MIT
Copyright (c) 2016-present, “KimKevin” Yongjun Kim
Here is a usage example in a Fragment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
public class FoodFragment extends Fragment { private static final String TAG = FoodFragment.class.getSimpleName(); private static String ARG_POSITION = "position"; private TextView txt; private Button btn; private KoreanFood koreanFoodItem; public static FoodFragment newInstance(int position) { FoodFragment fragment = new FoodFragment(); Bundle args = new Bundle(); args.putInt(ARG_POSITION, position); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { final int position = getArguments().getInt(ARG_POSITION); koreanFoodItem = CachePot.getInstance().pop(position); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_food, container, false); txt = (TextView) rootView.findViewById(R.id.main_txt); btn = (Button) rootView.findViewById(R.id.main_btn); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if (koreanFoodItem == null) return; txt.setText("It's " + koreanFoodItem.getName()); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CachePot.getInstance().push(koreanFoodItem); NaviUtil.addFragment(getActivity(), new FoodDetailFragment()); } }); } } |
Here is another example usage with a page adapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public class JarFragment extends Fragment { private static final String TAG = JarFragment.class.getSimpleName(); private ViewPager mViewPager; private List<KoreanFood> foodItems; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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")); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); mViewPager = (ViewPager) rootView.findViewById(R.id.main_pager); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); PagerAdapter mPagerAdapter = new PagerAdapter(getFragmentManager()); mViewPager.setAdapter(mPagerAdapter); } private class PagerAdapter extends FragmentStatePagerAdapter { public PagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { /** * Put your Obejct with Position to DataCache */ CachePot.getInstance().push(position, foodItems.get(position)); return FoodFragment.newInstance(position); } @Override public int getCount() { return foodItems.size(); } } } |
Find full example in the download