为片段提供初始数据的正确方法? [英] Proper way to give initial data to fragments?

查看:19
本文介绍了为片段提供初始数据的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我了解到我需要一个空的构造函数,以便我的片段不会在重新初始化时崩溃.我的问题是我在片段初始化时使用数据列表(至少其中一些).那么用数据列表开始新片段的好方法是什么?我应该在 OnCreate() 中创建一个从其他来源获取数据的 getData 方法还是什么是正确的方法?

So I've learned that I need an empty constructor in order for my fragments not to crash on reinitialization. My problem is that I use lists of data for my fragments when they are initialized (at least some of them). So what would be a good way to start new fragments with a list of data. Should I in the OnCreate() make a getData method which gets the data from some other source or what would be a proper approach?

因为我有很多数据,所以用数据来提供数据包真的不是一个很好的方法.

Feeding the bundle with data really wouldn't be a very good approach as I have a lot of data.

那么让我们举个例子(我这样理解得更好).

So let's take a case (I understand it tons better that way).

当用户点击按钮时,片段就会启动.我以前所做的是通过这种方式创建一个新片段:

When a user clicks on a button the fragment is started. What I used to do was creating a new fragment this way:

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    
    fragmentTransaction.replace(R.id.center_container, new DetailFragment(item));
    fragmentTransaction.addToBackStack(DETAIL_TAG);
    
    fragmentTransaction.commit();

然后在我的片段中:

public DetailFragment(EventItem item) {
    mItem = item;
    mPlaces = Database.getContainerPlaces(getActivity()).getValidItems();
}

我无法将所有数据都提供给一个包,所以这行不通.那我该怎么办?

I can't give all the data to a bundle, so that wouldn't work. So what should I do?

A:我是否应该使用空构造函数初始化片段,然后从我的活动中使用 setter 直接在片段中设置数据?但是,如果用户按下home键,Android关闭片段并且用户稍后返回,我会不会丢失数据?

A: Should I initialize the fragment with the empty constructor and then from my activity use setters to set the data directly in the fragment? However, won't I be missing data if the user presses home, Android closes the fragment and the user later returns?

B:我应该用工厂模式初始化片段并调用 setRetainInstance(true),给片段一个标识数据的键,然后让片段从一些获取 onCreateView 中所需的数据第三个来源?

B: Should I initialize the fragment with factory pattern and call setRetainInstance(true), give the fragment a key for identifying the data, and then letting the fragment fetch the data needed in onCreateView from some third source?

C:我应该创建一个空的构造函数,然后在 onCreate() 中获取片段所需的数据吗?

C: Should I just make an empty constructor and then in onCreate() fetch the data needed for the fragment?

应该注意的是,该应用是纵向锁定的,因此问题主要在于在 Android 关闭和用户重新启动时维护对象.

It should be noted that the app is locked in portrait so the issue is primarily with maintaining the objects when Android closes and the user restarts.

推荐答案

那么用数据列表开始新片段的好方法是什么.

So what would be a good way to start new fragments with a list of data.

使用工厂模式和参数"Bundle,例如:

Use the factory pattern and the "arguments" Bundle, such as:

package com.commonsware.empublite;

import android.os.Bundle;

public class SimpleContentFragment extends AbstractContentFragment {
  private static final String KEY_FILE="file";

  protected static SimpleContentFragment newInstance(String file) {
    SimpleContentFragment f=new SimpleContentFragment();

    Bundle args=new Bundle();

    args.putString(KEY_FILE, file);
    f.setArguments(args);

    return(f);
  }

  @Override
  String getPage() {
    return(getArguments().getString(KEY_FILE));
  }
}

如果你要保留你的片段实例,你应该能够只使用普通的 setter 将东西放入数据成员中.参数"Bundle 作为配置更改的一部分保留,因此对于非保留实例,这是确保在用户旋转屏幕等时保留设置数据的方法.

If you are retaining your fragment instance, you should be able to get away with just using ordinary setters to put stuff in data members. The "arguments" Bundle is retained as part of configuration changes, so for non-retained instances, this is the way to ensure your setup data is retained if the user rotates the screen, etc.

这篇关于为片段提供初始数据的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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