0

I am trying to call methodToBeCalled() from my UnrelatedClass. I am new to fragments, and 5 days later, still cannot figure it out. I would greatly appreciate any detailed help! If I did not include necessary code, please let me know and I will happily post it! Thanks!

public class MainActivity extends FragmentActivity{
    Fragment fragment = new FragmentActivity();
}

public class FragmentActivity extends Fragment{
    methodToBeCalled();
}

public class UnrelatedClass{
    //call FragmentActivity's methodToBeCalled() from here
    //note it is not static
}

The method methodToBeCalled() contains this code:

public void methodToBeCalled{
fragmentActivityTextView.setText("Something Else");
}

I have tried using this code in my UnrelatedClass, but unsuccessfully (althought I am not sure why):

    if (MainActivity.getCurrentFragment() instanceof FragmentActivity) {
        FragmentActivity fragment = (FragmentActivity) MainActivity.getCurrentFragment();
        fragment.methodToBeCalled();
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Your UnrelatedClass object needs to get a reference to the FragmentActivity object. How this should be totally depends upon what you want to do, and how you want to do it. Maybe the FragmentActivity creates the UnrelatedClass, and passes itsself to the class? What does the method do? – NickL Dec 24 '13 at 17:15
  • 1
    I understand you are trying to retrieve the FragmentActivity class and call a method, but what does the method do? Does it change the UI? Does it retrieve something from a web resource? Does it have anything to do with the content it is displaying? You are saying the class is unrelated, but how can a class be unrelated if it needs to call a method of the activity? – NickL Dec 24 '13 at 17:25
  • Oh, Gotcha. Thought it was irrelevant, my mistake. It updates the UI (TextViews). I'll update code above. – Evorlor Dec 24 '13 at 17:26

0 Answers0