call a main Activity method from a Fragment button

Asked

Viewed 1,836 times

1

I am using a standard android Activity, the "Navigation Drawer Activity", and There is a button in the navigation bar

public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    if (item.getItemId() == R.id.action_example) {
        Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT)
                .show();
        //getActivity().drawGraph();
        return true;
    }

And I would like to call the drawGraphic() method from my main Activity only when pressing the boot

It is worth mentioning that this navigation bar is a Fragment

1 answer

0


Try:

((Nome da Sua Activity)getActivity()).drawGraph();
  • 1

    @Andrey I’m not really sure if the above code solves the problem, but do a cast from a generic type to a specific type is an output to call some method of the specific type. However, I agree that the ideal would be for Matthew to explain the code so that the answer has a more comprehensive usefulness.

  • It worked. Thank you very much. Then I’ll research why it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.