简体   繁体   English

如何使用类名而不是索引来实例化片段类

[英]How to instantiate fragment class using class name instead of index

I have two fragment class named SessionTab and BillingTab and i am trying to create instance of those class using 我有两个名为SessionTabBillingTab片段类,我正在尝试使用这些类创建实例

SessionTab sessionTab = (SessionTab) getSupportFragmentManager().getFragments().get(1);

but sometimes index for those classes are reversed and then it causes ClassCastException 但有时这些类的索引会被反转,然后它会导致ClassCastException

How can i get instance of those fragment class by passing class name instead of index or any way to make sure that index of those class stays the same everytime so it doesn't cause ClassCastException 如何通过传递类名而不是索引来获取这些片段类的实例,或者以任何方式确保这些类的索引每次都保持不变,因此它不会导致ClassCastException

Use one of this methods : findFragmentById() and findFragmentByTag() methods. 使用以下方法之一: findFragmentById()findFragmentByTag()方法。

Reference : https://developer.android.com/reference/android/app/FragmentManager.html#findFragmentById(int) 参考: https//developer.android.com/reference/android/app/FragmentManager.html#findFragmentById(int)

Update : 更新:

ClassCastException is invoked when you are not casting the appropriate classes to one another. 如果未将适当的类转换为彼此,则会调用ClassCastException In your case, Your FragmentManager is returning different fragment than SessionTab , so the exception is thrown. 在您的情况下,您的FragmentManager返回与SessionTab不同的片段,因此抛出异常。

If you use findFragmentById() or findFragmentByTag() , then it will return the fragment exactly what you want, and exception will not be thrown. 如果你使用findFragmentById()findFragmentByTag() ,那么它将完全返回你想要的片段,并且不会抛出异常。

Define a 'tag' for the Fragment while adding it like 为片段定义'标签',同时添加它

 getFragmentManager().beginTransaction().add(new Fragment(),"your_tag");

And while referencing it use 虽然引用它使用

getFragmentManager().findFragmentByTag("your_tag");

In most cases, you would like to use YourFragment.class.getSimpleName() as your tag. 在大多数情况下,您希望使用YourFragment.class.getSimpleName()作为标记。

First of all, if you should understand that instance for any Fragment you can take from Java class api. 首先,如果您应该了解任何Fragment的实例,您可以从Java class api中获取。 Like below: 如下所示:

Class<?> class = Class.forName("example.package.BillingFragment");
Constructor<?> cons = class.getConstructor(BillingFragment.class);
BillingFragment object = (BillingFragment) cons.newInstance();

Code example show, how get Instance from any class in Java. 代码示例显示,如何从Java中的任何类获取实例。 But you talking a little bit other things. 但是你在谈论其他一些事情。 If I understand correct, you want to get Fragment from FragmentManager. 如果我理解正确,你想从FragmentManager获得Fragment。

You can do it, in case if you already defined Fragment before! 如果你之前已经定义过Fragment,你可以这样做! For example, you have base application flow, and then you want added Fragment. 例如,您有基本应用程序流,然后您想要添加Fragment。 You can check FragmentManager if there are Fragments in stack. 如果堆栈中有碎片,您可以检查FragmentManager。 But in case of empty stack, you should manually add them: 但是在空堆栈的情况下,您应该手动添加它们:

String billingFragmentTag = BillingFragment.class.getSimpleName();

......

if (getFragmentManager.findFragmentByTag(billingFragmentTag) == null) {
  BillingFragment fragment = new BillingFragment();
  String billingFragmentTag = BillingFragment.class.getSimpleName();

  FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
           fragTrans.add(fragment, billingFragmentTag).commit();
}

......

So after this, you can check if there your Fragment in stack and hook this active instance. 因此,在此之后,您可以检查堆栈中是否存在Fragment并挂钩此活动实例。 This is correct and standard flow for using Fragments. 这是使用Fragments的正确和标准流程。

......

if (getFragmentManager.findFragmentByTag(billingFragmentTag) != null) {
  BillingFragment fragment = getFragmentManager.findFragmentByTag(billingFragmentTag);
  String billingFragmentTag = BillingFragment.class.getSimpleName();

  FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
           fragTrans.add(fragment, billingFragmentTag).commit();
}

....

Welcome! 欢迎!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM