setRetainInstance(true),但不显示UI

时间:2014-04-20 18:49:26

标签: android gridview android-fragments

我正在尝试创建一个图片库,使用this tutorial作为指导,并进行了一些调整。

所以这是我的片段代码的一部分:

public static class PlaceholderFragment extends Fragment {

    private static final String TAG = "com.example.imageencryptor.RetainedFragment";

    LruCache<String, Bitmap> cache;

    public PlaceholderFragment() {

        initCache();

    }

    public void initCache() {
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;
        cache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return ImageLoader.getSizeInBytes(bitmap) / 1024;
            }
        };

    }

    public static PlaceholderFragment findOrCreateRetainFragment(
            FragmentManager fm) {
        PlaceholderFragment fragment = (PlaceholderFragment) fm
                .findFragmentByTag(TAG);
        if (fragment == null) {
            fragment = new PlaceholderFragment();
            fm.beginTransaction().add(fragment, TAG).commit();
        }
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        GridView imageGrid = (GridView) rootView
                .findViewById(R.id.gridview);

        ImageLoader loader = new ImageLoader(getActivity(), cache);
        imageGrid.setAdapter(new ImageAdapter(getActivity(), loader));

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

}

我的活动onCreate方法如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PlaceholderFragment.findOrCreateRetainFragment(getSupportFragmentManager());
    }

但是,我在启动时最终会出现空白UI。没有网格视图。这是为什么?

另一方面,如果我的onCreate改为:

,它将起作用
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PlaceholderFragment pFragment = PlaceholderFragment
            .findOrCreateRetainFragment(getSupportFragmentManager());


     if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction()
     .add(R.id.container, pFragment,PlaceholderFragment.TAG).commit();
     }

}

但是,当屏幕方向发生变化时,不一定会在FragmentManager中反复添加多个相同的片段和相同的标签吗?

1 个答案:

答案 0 :(得分:0)

我猜您的UI不会显示,因为您在名为FragmentTransaction的界面中执行了findOrCreateRetainFragment。此接口用于将数据从Activity传递到动态添加的Fragment,即使片段以某种方式由Android重新创建,也可以使用此接口。您需要在Activity中执行FragmentTransaction,如下所示:

// Init your fragment
PlaceholderFragment pFragment = (PlaceholderFragment) 
    getSupportFragmentManager().findFragmentByTag(PlaceholderFragment.TAG);

// If it is null: the fragment is not created yet - create it
if(pFragment == null) {
    // send data to interface - which will create a new one
    PlaceholderFragment pFragment = 
            new PlaceholderFragment().instanceRetainFragment(value);
    // add the new fragment
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, pFragment, PlaceholderFragment.TAG).commit();
}
// else: avoid to create the fragment again

然后,片段中的界面可能是这样的:

public static PlaceholderFragment instanceRetainFragment(int value) {
    // create a new fragment
    PlaceholderFragment mFragment = new PlaceholderFragment();
    // create a bundle to store data
    Bundle args = new Bundle();
    // insert data
    args.putInt("mValue", value);
    // attach data
    mFragment.setArguments(args);
    // return new fragment in the activity
    return mFragment;
}

您也可以创建一个界面而不存储数据,只需调用第一行和最后一行。但是,从您的活动中存储和接收一些信息可能很有用(例如:Boolean来过滤您的GridViewInteger只显示一个类别等等。)<登记/> 因此,主要问题是在Fragment中调用FragmentManager而不调用Activity。这可能是&#34;没有显示UI&#34;的原因。如果有效,请告诉我。