0

To put you in context, when the user clicks the floating action button the activity will check which fragment is active.

Then, depending on the fragment tag it will call a method in that specific instance.

I found a way to do it on this awesome forum but the only problem is that I don't understand what I'm doing here

       fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);

            if (fragment.getTag() == "EXERCISES_LIST_FRAGMENT") {

                //THIS IS THE PART I DONT UNDERSTAND
                ArrayList t = ((ExercisesListFragment) fragment).GetArray();
                //THIS IS THE PART I DONT UNDERSTAND


                System.out.println(t.size());
            }
        }
    });

It seems like casting a variable but I searched and did not see anything like this.

I want to know what is that part : ((SomeKindofClass) variable).SomeKindOfClassMethod();

I would like to read about that but I don't know how you name that

sorry for my English

Thanks

Popo
  • 1
  • 2
    You hit it right on the head; it's casting. Look [here](http://stackoverflow.com/questions/20096297/explicit-type-casting-example-in-java) for more info. The second part of that statement is simply calling a method on the casted object – Michael Jun 03 '16 at 03:06
  • Consider creating a `BaseFragment` that your fragments extend from. `getArray()` could be an abstract in the base fragment class that each child fragments would implement. this may be cleaner and you don't need to check for tag name or cast fragment before calling `getArray()`. By the way, `getArray()` is a pretty terrible name as it is not clear what it does. – liminal Jun 03 '16 at 03:33
  • Thanks both of you!. Read a bit and it helped me understand better what I was doing here – Popo Jun 03 '16 at 09:57

0 Answers0