如何恢复Android的片段视图状态 [英] How to restore Android fragment view state

查看:149
本文介绍了如何恢复Android的片段视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的标题片段和内容屏幕上的片段。当在标题的用户点击项目片段的片段,根据是创建并插入到一帧和所选择的标题在标题片段高亮

I have application titles fragment and contents fragment on the screen. When the user clicks on the item in titles fragment the according fragment is created and inserted to a frame and the selected title is highlighted in titles fragment.

交易符合 fragment.addToBackStack(),因此当用户点击返回键,$做p $ pvious片段被恢复并插在帧

Transaction is done with fragment.addToBackStack(), so when the user clicks the BACK key, the previous fragment is restored and inserted in the frame.

什么是恢复视图状态时,事务管理器恢复片段的最佳解决方案?

What is the best solution to restore view state when transaction manager restores fragments?

现在的问题是,我应该突出标题片段previous片段的名字,我应该知道这是什么片段。创建片段时,恢复对不断变化的交易backstack使用事务经理听众:我通过存储视图状态在我自己的堆栈解决它。

The problem is that I should highlight previous fragment name in titles fragment and I should know what fragment it is. I resolved it by storing view state in my own stack: when fragment is created and restoring on changing transaction backstack using transaction manager listener.

但是,这似乎不是正确的解决方案。

But this doesn't seem like the correct solution.

推荐答案

答案之前,下次记得要加你的code。机会是我的回答不会帮助你,它更可能是因为我真的不知道你的code。

Before the answer, next time remember to add your code. Chances are my answer will not help you as much as it could because I don't really know your code.

这是旧的,但无论如何,如果我没有理解你的问题(和应用程序的体系结构),这听起来像用于连接的情况。

This is old but anyway, IF I understand your question (and app architecture) correctly, it sounds like a case for interfacing.

例如:

添加此接口为成员的内容片断:

add this interface as a member to the content fragment:

public class ContentFragment extends Fragment{

   public interface onFragmentTitleHighlighted{
      public void highLightTitle(String title);
   }
}

和有标题的片段实现它。确保装备你的内容片段,标题fragmnet,并添加调用 highLightTitle(字符串名称); 中的内容片段的onCreateView(...)调用。这种方式,每当新的内容fragmnet被添加的标题将被突出显示。

and have the title fragment implement it. Make sure to equip your content fragments with the title fragmnet, and add a call to highLightTitle(String title); in the content fragment's onCreateView(...) call. This way whenever a new content fragmnet is being added the title will be highlighted.

这篇关于如何恢复Android的片段视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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