在一个标签主机的Andr​​oid碎片视图状态 [英] Android Fragment view state within a tab host

查看:121
本文介绍了在一个标签主机的Andr​​oid碎片视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/6987334/separate-back-stack-for-each-tab-in-android-using-fragments">Separate返回堆栈使用片段在Android中每个选项卡

我最近开始工作,其选项卡主机内的碎片的应用程序FragmentActivity内.. Android的文件说,要做到这一点的TabActivity类现在是很precated。为了使用的片段,我现在用的是android的V4支持库。

目前我的应用程序包括在其中的片段中存在一个选项卡。导航到新的片段选项卡中我一直在使用下面的code(这似乎根据的文件是正确的):

  FragmentTransaction英尺= fragmentManager.beginTransaction();
ft.replace(containerId,newFragment);
ft.addToBackStack(空);
ft.commit();
 

一切都很好,和良好的,直到我打了返回键导航至previous片段,并意识到,我所有的视图状态有pretty的很多被摧毁...发生了什么事是,onCreateView()方法被再次呼吁我的previous片段,尽管其意见有pviously创建$ P $ ......这是为什么?我已搜索各地对如何正确维护状态的片段,但我似乎无法找到这个体面的文件。

那么,什么我想是使用

  ft.add(containerId,newFragment);
 

而不是

  ft.replace(containerId,newFragment);
 

这其实相当奏效的,我的视图状态保留 - onCreateView()没有打电话给我的previous片段显示各一次。不过,后来我开始注意到的是,我可以从previous片段组件(按钮等)躺在当前片段下方互动,尽管我看不到的组件!?我读的地方,下面的方法可以用于保存一些状态:

 公共无效的onSaveInstanceState(包outState)
 

我实现了这个方法,但是,这是从来没有叫!我还注意到,捆绑过去到onCreateView()和onActivityCreated()方法总是空。当然,谷歌还没有今天这一步很难做到这样的事情,我必须失去了一些东西?如何正确维护一个片段的视图状态的任何帮助,将大大AP preciated!

感谢。

解决方案

看一看这个问题 - 我认为两者可能有关系,你可能能够解决此问题

标签在Android中使用片段

Possible Duplicate:
Separate Back Stack for each tab in Android using Fragments

I've recently started working on an application with fragments inside a tab host within a FragmentActivity.. the Android documentation said to do this as the TabActivity class is now deprecated. In order to use fragments, I am using the android v4 support library.

Currently my app consists of one tab within which the fragments exist. To navigate to new fragments within the tab I have been using the following code (which seems to be correct based on the documentation):

FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(containerId, newFragment);
ft.addToBackStack(null);
ft.commit();

All was well and good until I hit the back key to navigate to a previous fragment and realised that all of my view state had pretty much been destroyed.. what was happening is that the onCreateView() method was being called again on my previous fragment even though its views had previously been created... why is this? I've searched around on how to properly maintain state with fragments but I can't seem to find decent documentation on this.

What I then tried was to use the

ft.add(containerId, newFragment);

instead of

ft.replace(containerId, newFragment);

This in fact worked quite well in that my view state was retained - onCreateView() was not called each time my previous fragments were shown. However, what I then began to notice was that I could interact with components (button, etc.) from previous fragments lying below the current fragment, even though I couldn't see the components!? I read somewhere that the following method should be used to save some state:

public void onSaveInstanceState (Bundle outState)

I implemented this method, however, it was never called! I also noticed that the bundles past to the onCreateView() and onActivityCreated() methods are always null. Surely Google has not made it this difficult to achieve something like this and I must be missing something? Any help on how to correctly maintain a fragment's view state will be greatly appreciated!

Thanks.

解决方案

Have a look at this question - I think the two may be related and you might be able to work around this issue.

Tabs in Android using Fragments

这篇关于在一个标签主机的Andr​​oid碎片视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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