Search code examples
javaandroidandroid-fragmentsinstance-variablesfragmenttransaction

How to add an instance variable to a base class in Java (in my case, FragmentTransaction)


I'm new in android programation and I would like to know if it is possible to add an instance variable to a default class such as FragmentTransaction, if yes, how could I do that?

I've search on the internet but I found nothing (maybe I didn't search well).

I would like to add a boolean instance variable to FragmentTransaction, this variable would become true in certain cases and it would differanciate fragmentTransaction which need to be popped from others. Thank you :)


Solution

  • I would like to know if it is possible to add an instance variable to a default class such as FragmentTransaction

    No, sorry.

    In some cases, you can create a subclass and put your data there. However, you do not create instances of FragmentTransactionFragmentManager does that for you. So there is no opportunity for you to use a subclass.

    I would like to add a boolean instance variable to FragmentTransaction, this variable would become true in certain cases and it would differanciate fragmentTransaction which need to be popped from others.

    You may be better served by using the Navigation component from the Jetpack libraries, so your rules regarding fragment navigation get encoded in your navigation graph.