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

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

问题描述

我需要一个应用程序,它应该有一个 UI 工作流,用户应该能够浏览应用程序的特定部分,可以是 ListView 或 GridView,并且他可以点击一个项目来显示详细信息特定项目.现在如果用户向右滑动即ViewPager"视图寻呼机应该根据用户滑动的方向更改片段以显示上一个列表中的下一个或上一个项目,同样当用户按下详细项目视图时,现有的 ViewPager 应该关闭,上一个 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 和 Approach 应该读取和写入相同的数据结构,并且它们应该同步,以便在一个屏幕上启动加载更多数据时,同时如果用户选择一个特定的项目,下一个视图应该在上一个屏幕加载完成后自动更新数据.

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.

就像 Fancy9gag

编辑:我不想维护数据库,我只需要在我的应用程序进程处于活动状态之前访问数据.

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

推荐答案

Android 使用 Bundle 提供从 String 值到各种 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);

对于 Fragment 使用 Bundle:-

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.

请按照以下步骤操作:-启动应用程序池:-

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

重要说明:- 获取数据后通知listview或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天全站免登陆