正确的方式给初始数据片段? [英] Proper way to give initial data to fragments?

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

问题描述

所以,我明白了,我需要一个空的构造为了我的碎片不会崩溃的重新初始化。我的问题是,我用我的碎片数据列表时,它们被初始化(至少其中的一些)。因此,这将是开始新的片段,数据列表的好方法。我在OnCreate()应该做出的getData方法,从其他来源或者是获取数据将是一个适当的解决办法?

送料包用数据真的不会是一个很好的方法,因为我有很多的数据。

所以,让我们的情况(我的理解吨这样更好)。

当用户点击该片段开始的按钮。我以前做的是创造一个新的片段是这样的:

  FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.replace(R.id.center_container,新DetailFragment(项目));
    fragmentTransaction.addToBackStack(DETAIL_TAG);

    fragmentTransaction.commit();
 

然后在我的片段:

 公共DetailFragment(EventItem项目){
    MITEM =项目;
    。mPlaces = Database.getContainerPlaces(getActivity())getValidItems();
}
 

我不能给所有的数据到捆绑,这样是行不通的。所以,我应该怎么办?

答:我应该初始化与空构造函数,然后从我的活动中使用的setter片段直接在片段设置的数据?然而,将不我丢失的数据,如果用户presses家,Android的关闭片段和用户以后的回报?

B:我应该初始化片段,工厂模式和调用setRetainInstance(真),给片段密钥识别数据,然后让碎片取一些第三方源onCreateView所需要的数据。

C:如果我只是做一个空的构造函数,然后在的onCreate()获取所需片段中的数据

应当指出的是,该应用被锁定在纵向所以这个问题是主要与维护对象时机器人关闭,并且用户将重新启动。

解决方案
  

那么,这将是开始新的片段,数据列表的好方法。

使用工厂模式和论据捆绑,如:

 包com.commonsware.empublite;

进口android.os.Bundle;

公共类SimpleContentFragment扩展AbstractContentFragment {
  私有静态最后弦乐KEY_FILE =文件;

  受保护的静态SimpleContentFragment的newInstance(字符串文件){
    SimpleContentFragment F =新SimpleContentFragment();

    捆绑的args =新包();

    args.putString(KEY_FILE,文件);
    f.setArguments(参数);

    返程(F);
  }

  @覆盖
  串GETPAGE(){
    返程(getArguments()的getString(KEY_FILE));
  }
}
 

如果您保持您的片段实例,你应该能够得到与只使用普通的制定者把东西在数据成员。在参数捆绑如果用户旋转屏幕被保留作为配置更改的部分,因此对于非保留的情况下,这是方式,以确保您的设置数据保留等等。

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

Then in my fragment:

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: 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 close the fragment and the user later returns?

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: Should I just make an empty constructor and then in onCreate() fetch the data needed for the fragment?

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.

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

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天全站免登陆