Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
No Result
View All Result
Home Android Activity

CachePot

Android Data Object Cache Management

2 weeks ago
in Activity, Routing, Fragment
Reading Time: 1min read
40 0
A A
40
SHARES
4k
VIEWS
Share on FacebookShare on Twitter

Questions CachePot helps answer.

Here is a summary of the problems cachepot helps you answer:

SimilarPosts

LifecykleLog

1 week ago
4k

Activity State Changes

1 week ago
4k

Activity Lifecycle Tutorial

1 week ago
4k

Introduction to Activities

1 week ago
4k
  1. How to Pass Data Between Activities
  2. How to Pass Data Between Fragments
  3. How to pass multiple data types between activities/fragments.
  4. How to Pass Data Between Activities and Fragments
  5. How to Pass Data From PageAdapter to Fragments.

Difference to other similar Libraries

  1. Its easy to use.
  2. Complete, encompassing both fragments and activities.
  3. Ability to pass single or multiple data or types. Easily pass custom objects or collections for example.
  4. No annotations or extension of classes.
DescriptionInfoDownloadExample

CachePot - Android Simple Data Cache

Build Status Download API

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 and Activity
  • Between Activity and Fragment
  • Between Fragment and Fragment
  • From PagerAdapter to individual Fragment

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

Android Data Object Cache Management
Library written By: kimkevin kimkevin

Created: 2015-09-21T15:45:22ZLast Update: 2020-09-11T09:13:24Z

Stars: 38Language: JavaForks: 13
View in Github

Download

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

 

Tags: Data Passing
Share16Tweet10Send

Related Posts

Lifecycle

LifecykleLog

February 16, 2021
4k
Activity Basics

Activity State Changes

February 16, 2021
4k
Lifecycle

Activity Lifecycle Tutorial

February 16, 2021
4k
Activity Basics

Introduction to Activities

February 16, 2021
4k
Lifecycle

RxLifecycle

February 16, 2021
4k
Routing

ActivitySwitcher

February 15, 2021
4k

Categories

  • Android
    • Introduction
    • Activity
      • Activity Basics
      • Lifecycle
      • Routing
    • Fragment
    • UI
      • AppBar and Toolbar
      • TextView
      • Tabs
      • Dialog
      • CardView
      • Chips
      • EditText
      • Navigation Drawer
      • Button
      • Bottom Navigation
    • Animation
    • MediaPlayer
      • VideoPlayer
    • Biometric
    • Jetpack
    • Android Firebase
    • Layouts
    • JSON
    • ContentViews
    • RxJava
    • PinView
    • Utility
    • ScrollView
    • URL
    • Log
    • Intent
    • DateTime
    • DownloadManager
    • Camera
    • SharedPreferences
    • SQLite
    • Retrofit
    • RecyclerView
    • Bluetooth
    • Hardware and Sensors
    • HTTP
    • Android AdapterViews
    • Projects
    • InputViews
    • Navigation
    • Menu
    • System
    • Communication
    • HttpURLConnection
    • Fast Networking Library
    • FileSystem
    • Components
    • Threading
    • Local Database
    • Jetpack
  • Flutter
    • Flutter HTTP
    • Flutter Widgets
    • Flutter Firebase
  • Java
    • Java Threading
    • Java Collections
    • JavaFX
  • Programming
  • Flutter Projects
  • Java DateTime
  • Flutter Listing Views
  • Flutter Image
  • Flutter Local Database
  • Documentary
  • Meditation
  • Flutter Animation and Motion
  • Javascript
    • Framework7
  • Xamarin Android
  • Python
    • CherryPy

Get Free Projects

  • Home
  • Android
  • Buy Projects
  • My Account

© 2021 Camposha

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

© 2021 Camposha

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In