本文介绍了findFragmentById()和findFragmentByTag()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我创建了一个片段来展示我的回收商视图。所以我使用了 findFragmentById()方法来查找我的xml文件。问题在于,我每次旋转屏幕时,都会创建另一个再循环视图堆栈。 这里是我的代码: @Override 保护无效的onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListFragment savedFragment =(ListFragment)getSupportFragmentManager()。findFragmentById(R.id.list_recyclerview); if(savedFragment == null) { ListFragment fragment = new ListFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.place_holder,fragment); fragmentTransaction.commit(); $ b 但是当我使用方法 任何人都可以向我解释这两种方法有什么区别吗? findFragmentByTag() b $ b 解决方案 findFragmentByTag : 以前添加的带有给定标签的片段的实例。这是通过以下方式完成的: MyFragment fragment = new MyFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.place_holder,fragment,myFragmentTag); fragmentTransaction.addToBackStack(myFragmentTag); fragmentTransaction.commit(); 然后检索片段的实例: fragment =(MyFragment)getSupportFragmentManager()。findFragmentByTag(myFragmentTag); findFragmentById : 在这个方法中,您将获取可见片段的实例,在这种情况下,将获取片段容器中的片段。将片段添加到没有标记的容器中: MyFragment fragment = new MyFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.place_holder,fragment); fragmentTransaction.commit(); 然后检索它的实例,您使用容器标识,而不是回收视图标识: fragment =(MyFragment)getSupportFragmentManager()。findFragmentById(R.id.place_holder); 如果您不想要 onCreate 在屏幕上运行,您应该添加到清单中,在 Activity 语句中,如下所示: android:configChanges =orientation | screenSize 这段代码可以确保,在循环中你的 Activity 不会被重新创建并保持它的状态。考虑到这可能会混淆你的代码逻辑。要处理布局中的旋转变化,您应该在 onConfigurationChanged 中加入I created a Fragment to display my recycler view. so I used the method findFragmentById() to find my xml file. The problem is each time i rotated the screen, it created one more recycler view stacks on top of one another.here is my code:@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListFragment savedFragment = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list_recyclerview); if(savedFragment == null) { ListFragment fragment = new ListFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.place_holder,fragment); fragmentTransaction.commit(); }}But when I used the method findFragmentByTag(), it didn't happened.May anyone explain to me what are the difference between those 2 methods? 解决方案 findFragmentByTag:This method allows you to retrieve the instance of a previously added fragment with the given tag. This is done the following way:MyFragment fragment = new MyFragment();FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.add(R.id.place_holder,fragment,"myFragmentTag");fragmentTransaction.addToBackStack("myFragmentTag");fragmentTransaction.commit();And then to retrieve the fragment's instance:fragment = (MyFragment) getSupportFragmentManager().findFragmentByTag("myFragmentTag");findFragmentById:In this method you are going to get the instance of the visible fragment, in this case, the fragment that is in the fragment container. Adding the fragment to the container without tag:MyFragment fragment = new MyFragment();FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.add(R.id.place_holder,fragment);fragmentTransaction.commit();And then to retrieve it's instance you use the container id, not the recycler view id:fragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.place_holder);For further information regarding your problem, if you don't want onCreate to run on screen rotation you should add to your manifest, inside your Activity statement, the following:android:configChanges="orientation|screenSize"This code ensures, that on rotation your Activity won't be recreated and will keep it's state. Take in consideration that this might mess with your code logic. To handle the rotation changes in the layout you should do it inside onConfigurationChanged 这篇关于findFragmentById()和findFragmentByTag()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 09:15