什么是分享不同的活动或片段之间数据的正确方法是什么? [英] What is the correct way to share data between different Activities or Fragments?

查看:128
本文介绍了什么是分享不同的活动或片段之间数据的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个应用程序,它应该有一个UI工作流程,在用户应该能够浏览该应用程序的特定部分,它可以是一个ListView或GridView和在那里,他可以在一个项目挖掘揭示的细节特别item.Now如果用户扫描到的右左,即ViewPager视图寻呼机应该改变片段以显示下一个或previous项在previous列表取决于用户轻扫的方向,还可以当用户pressed背面上的详细项目查看现有ViewPager应关闭且所述previous的ListView或GridView的应该显示和View的位置应设置为用户看着在ViewPager的项目

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.

要保持简单,高效的两种观点,即ListView和途径应该读写同样的数据结构,他们应该是同步的,这样当更多的数据在一个屏幕上,并在此同时,如果启动负载用户选择特定项目的下一个视图加载在previous屏幕完成后,应自动更新数据。

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.

就像花式或的 9gag

修改:我不希望保持一个数据库,我需要访问的数据只到我的应用程序的过程是活着

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

推荐答案

Android提供的字符串值映射到使用捆绑各种Parcelable类型。

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

有关的活动: -

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);

修改为您的方案:我觉得更好的选择是创建ApplicationPool。

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

按照下面的步骤: -  启动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");

重要注意事项: - 获取数据后通知列表视图或GridView控件

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

ApplicationPool类文件

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);

    }
}

这篇关于什么是分享不同的活动或片段之间数据的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆