1

I have 1 main activity and 4 fragments. Each fragment have the same method name, each does a different thing (initializing data is the same, what each fragment does with the data is different).

At the moment I check which fragment is active and I call that method:

if (getSupportFragmentManager().findFragmentByTag("f1") != null)
   Objects.requireNonNull((FragmentF1) getSupportFragmentManager().findFragmentByTag("f1")).setupData(true);
else if (getSupportFragmentManager().findFragmentByTag("f2") != null)
   Objects.requireNonNull((FragmentF2) getSupportFragmentManager().findFragmentByTag("f2")).setupData(true);

I want to call that method, no matter the fragment I have now. Is it possible?

Also, the requireNonNull is there to avoid the lint warning despite the null check I do one line above, is there a way to make this code cleaner?

Amos
  • 1,321
  • 2
  • 23
  • 44
  • Can you elaborate on what you mean by: "I want to call that method, no matter the fragment I have now". Do you want a single method that does everything? Or, have you consolidated the functionality of each method into a single fragment? – BlackHatSamurai Jul 12 '19 at 17:33
  • I have setupData() method in each fragment that initialize the data and "do something" with it. The "do something" is different per fragment. I was wondering whether I can just call that method (name) without "searching" for the active fragment. – Amos Jul 12 '19 at 17:36
  • Where do you want to call the method? In the fragment itself? – BlackHatSamurai Jul 12 '19 at 18:13
  • From MainActivity to a fragment it holds – Amos Jul 12 '19 at 18:22

3 Answers3

1

You can create base class or abstract class for your fragments

public abstract class BaseFragment extends AppCompatActivity {

    abstract void setupData(boolean b);
}

Then each fragments inherit from BaseFragment class and overrides setupData(boolean b). After you find the fragment check if he instace of BaseFragment and call the method

Fragment fragment = getSupportFragmentManager().findFragmentByTag("tag")

    if (fragment instanceof BaseFragment){
         fragment.setupData(true);
       }
Pavel Poley
  • 5,307
  • 4
  • 35
  • 66
  • Assuming all fragments inherit from BaseFragment, is there a way to avoid "searching" for the fragment and just do something like (BaseFragment) getSupportFragmentManager.ActiveFragment.setupData()? – Amos Jul 12 '19 at 17:33
  • it is another problem, you need to track the current fragment, check this answer https://stackoverflow.com/a/24589081/5827565 – Pavel Poley Jul 12 '19 at 17:35
  • That IS my problem :-) so if I understand correctly, I'm not searching for the fragment, I'm taking the container, and use the BaseFragment to call that "same" method, no matter the fragment that is currently active. Sounds promising, will give it a try, thanks. – Amos Jul 12 '19 at 17:40
  • I guess there are several methods to track current fragment, its depends on situation, but if you have 4 fragments in same container that can be solution in your case – Pavel Poley Jul 12 '19 at 17:42
0

you can declare this method in your activity in which take fragment as parameter and in each time you call this method check on passed fragment and do your logic based on it.

public static void initData(Fragment fragment){
    if (fragment instanceof FirstFragment){
        // Do your logic here
    }else if(fragment instanceof SecondFragment){
        // Do your logic here
    }
}//initData()
Ahmed Abdalla
  • 2,356
  • 2
  • 14
  • 27
  • Thanks, that leads me to the same place where I need to check which fragment I need to "use". I was wondering if there is something more generic like getSupportFragmentManager().activeFragment.setupData(); – Amos Jul 12 '19 at 17:30
0

When you should call the method ?

I think in your cause you should use Event bus, so with eventbus when something happen you should post the event to the active fragment

EventBus.getDefault().post("your event here");

and register the event in the fragments so when the fragment is currently launched and the event posted the fragment will receive the event

read more about event bus

huda Olayan
  • 143
  • 1
  • 13
  • You wrote "post the event to the active fragment" the question is whether I will need to "search" the active fragment, if so, it will be pretty much the same as I have now. – Amos Jul 12 '19 at 17:35
  • you only have to post the event and the active fragment will receive it without detrmine which one is the active – huda Olayan Jul 12 '19 at 18:12