Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to share data between different Activities or Fragments?

I need an app that should have a UI workflow where in a user should be able to browse a particular section of the app which can be either a ListView or a GridView and where he can tap on an item to reveal details of that particular item.Now if the user swipes to the left of the right “i.e. ViewPager” the View pager should change fragment to reveal the next or previous item in the previous List depending on the direction of the User’s swipe,also when the user pressed back on the detailed Item view the existing ViewPager should be closed and the previous ListView or GridView should be shown and the position of the View should be set to the item the user was looking at in the ViewPager.

To keep things simple and efficient the two views i.e. ListView and the Approach should read and write to the same data structure and they should be in sync such that when the load more data is initiated on one screen and in the meanwhile if the user selects a particular item the next view should update the data automatically after loading completes in the previous screen.

enter image description here

Just like Fancy or 9gag

EDIT: I do not want to maintain a database, I need access to data only till my application's process is alive.

like image 909
Jitender Chaudhary Avatar asked Oct 24 '25 04:10

Jitender Chaudhary


1 Answers

Android provides mapping from String values to various Parcelable types using Bundle.

For activity:-

Intent in = new Intent(Sender.this, Receiver.class); 
in.putString(key, value)
startActivity(in);

For Fragment use Bundle:-

Fragment fragment = new Fragment(); 
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Edit for your scenario : I think the better option is create ApplicationPool.

Follow the below steps:- Initiate the ApplicationPool :-

ApplicationPool pool = ApplicationPool.getInstance();

modify the data on details page and add to pool

pool.put("key", object);

get the modified data on list page from pool

Object  object = (Object) pool.get("key");

important notes:- notify the listview or gridview after getting the data

ApplicationPool class file

public class ApplicationPool {

    private static ApplicationPool instance;
    private HashMap<String, Object> pool;

    private ApplicationPool() {
        pool = new HashMap<String, Object>();
    }

    public static ApplicationPool getInstance() {

        if (instance == null) {
            instance = new ApplicationPool();

        }

        return instance;
    }

    public void clearCollectionPool() {
        pool.clear();
    }

    public void put(String key, Object value) {
        pool.put(key, value);
    }

    public Object get(String key) {
        return pool.get(key);
    }

    public void removeObject(String key) {

        if ((pool.get(key)) != null)
            pool.remove(key);

    }
}
like image 164
Tech_Intelliswift Avatar answered Oct 26 '25 22:10

Tech_Intelliswift



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!