Visit Sponsor

Written by 10:35 am Android

How to Launch an Activity in Android

For launching an activity, we need to create an explicit intent that defines the activity that we wish to start. In the below code snippet, the first parameter to Intent constructor is the current activity context and the second parameter is your new activity class. The startActivity() method can be called on Activity context.

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

If you want to start an activity from fragment.

Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startActivity(intent);

Visited 7 times, 2 visit(s) today
Close