从后台获取片段 [英] Get fragment from backstack

查看:109
本文介绍了从后台获取片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎不可能从后台获得片段,甚至开始考虑留在单身人士中,这可能是不好的。
保存到这样的backstack,所有的时间试图通过标签或东西给我错误。

 碎片碎片= UserProfileFragment.newInstance(null); 
FragmentTransaction trans = getFragmentManager()。beginTransaction();
trans.replace(FRAGMENT_PLACE_RESOURCES,fragment);
trans.addToBackStack(profile);
trans.commit();

它只是在这里返回null,所以我不能使用这个片段。没有日志。

 片段fragment2 = getFragmentManager()。findFragmentByTag(profile); 


解决方案> getFragmentManager()。findFragmentByTag (tag)



仅用于添加具有特定标记的片段,例如



fragmentTransaction.add(R.id.order_container,mProfileFragment,profile);





fragmentTransaction.replace(R.id.order_container,mProfileFragment,sometag);



然后你就可以通过标签找到这个片段。



在你的情况中您正在向叠堆添加事务,因此您将无法通过标记找到该碎片。你只是添加一个事务来堆叠它不是一个片段。而且你的片段被从活动中删除并销毁,因此你必须通过弹出backstack来恢复事务,而不是通过标记找到该片段。
您必须调用
$ b

getFragmentManager()。popBackStack(profile);



将片段返回到活动并使其在屏幕上可见。


Hardly can't get fragment from backstack, even start thinking of keeping in in singleton which is probably bad. Saved to backstack like this and all time try to get it by tag or something gives me error.

Fragment fragment = UserProfileFragment.newInstance(null);
                        FragmentTransaction trans = getFragmentManager().beginTransaction();
                        trans.replace(FRAGMENT_PLACE_RESOURCES, fragment);
                        trans.addToBackStack("profile");
                        trans.commit();

It just return me null here so I can't use this fragment. No logs.

Fragment fragment2 = getFragmentManager().findFragmentByTag("profile");

解决方案

getFragmentManager().findFragmentByTag("tag")

is only used when you have added a fragment with specific tag for e.g.

fragmentTransaction.add(R.id.order_container,mProfileFragment,"profile");

or

fragmentTransaction.replace(R.id.order_container,mProfileFragment,"sometag");

Then you will be able to find this fragment by the tag.

In your case you are adding a transaction to backstack so you will not be able to find that fragment by tag. You just adding a transaction to backstack thats it not a fragment. And also your fragment was removed from the activity and destroyed so you have to revert the transaction by popping backstack instead of finding that fragment by tag. You have to call

getFragmentManager().popBackStack("profile");

to get that fragment back to the activity and make it visible on screen.

这篇关于从后台获取片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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