How To Open Another Activity Click On Fragment Button Androidpro.in

Open Another Activity Using Fragment Button

In this post we will learn how to open new activity click on button in android studio, when I press the button to go into another Activity. I use activity & fragment first fragment where show button for click and second activity is used for open new activity, for more explain watch video below.


Here's the Java code:

1st Method

public class About extends Fragment {

    Intent intent;

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

 

        View rootView = inflater.inflate(R.layout.about, container, false);

 

        intent = new Intent(getActivity(), Contact_Developer.class);

        final Button button = (Button) rootView.findViewById(R.id.btnContactDev);

 

        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                startActivity(intent);

            }

        });

 

        return rootView;

    }

}

 

 

2nd Method

public class About extends Fragment {

 

    public void onCreate (Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

 

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

 

        View rootView = inflater.inflate(R.layout.about, container, false);

 

        Button button = (Button) rootView.findViewById(R.id.btnContactDev);

        button.setOnClickListener(new View.OnClickListener() {

 

           @Override

           public void onClick(View arg0) {

              Intent intent = new Intent(getActivity(), Contact_Developer.class);

              getActivity().startActivity(intent);

           }

        });

        return rootView;

    }

}