Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate fragment class using class name instead of index

I have two fragment class named SessionTab and BillingTaband i am trying to create instance of those class using

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

but sometimes index for those classes are reversed and then it causes 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

like image 948
Skyyy Avatar asked Sep 21 '16 16:09

Skyyy


People also ask

How to include a fragment in an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

How add and remove fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

What is fragment activity in android studio?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


3 Answers

Use one of this methods : findFragmentById() and findFragmentByTag() methods.

Reference : 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. In your case, Your FragmentManager is returning different fragment than SessionTab, so the exception is thrown.

If you use findFragmentById() or findFragmentByTag() , then it will return the fragment exactly what you want, and exception will not be thrown.

like image 74
Ajay P. Prajapati Avatar answered Oct 17 '22 23:10

Ajay P. Prajapati


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.

like image 35
Zorawar Sachdev Avatar answered Oct 18 '22 00:10

Zorawar Sachdev


First of all, if you should understand that instance for any Fragment you can take from 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. But you talking a little bit other things. If I understand correct, you want to get Fragment from FragmentManager.

You can do it, in case if you already defined Fragment before! For example, you have base application flow, and then you want added Fragment. You can check FragmentManager if there are Fragments in stack. 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. This is correct and standard flow for using 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!

like image 33
GensaGames Avatar answered Oct 17 '22 23:10

GensaGames