-3

I want to call a class using button created in the fragment. This is the code for the fragment which contains the button.

public class FragmentSecondPage extends Fragment{

    View root;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){
        root = inflater.inflate(R.layout.fragment_page2, container, false); 
        return root;
    }
}

I want to start a class SplashActivity.class from the button present infragment_page2. Could somebody please help me?

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Anish688
  • 81
  • 11
  • first you can start using google: How to start new activity on button click. Then you can click on the first result which brings you to stackoverflow. http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click – alecnash Jan 26 '14 at 18:37
  • How is this different from you [previous question](http://stackoverflow.com/questions/21227412/trying-to-open-2-different-xml-page-using-2-different-buttons) that I answered? – takendarkk Jan 26 '14 at 18:44
  • the problem was that i was getting an error msg at the line Button bt = (Button) findViewById(R.id.button1); i had to convert it to Button bt = (Button) root.findViewById(R.id.button1); could you please tell me the difference for future reference and i got it running your code itself thank. – Anish688 Jan 27 '14 at 00:57

1 Answers1

2
//Create Intent that would call the Next Activity and since you are in Fragment you need to call getActivity() instead of this
Intent myIntent = new Intent(getActivity(), SplashActivity.class);
// This function will replace the currently running activity with new one
getActivity().startActivity(myIntent);