horbags
horbags

Reputation: 784

Android - Failure to launch an Activity via an Intent from a Fragment

I create a fresh project in Android studio using the wizard that creates a blank activity with a fragment. I then create a second basic activity with a fragment. In the first fragment I add a button, which has an OnClickListener, and inside of onClick() it does the following:

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

When I click the button, despite the code being run, the activity is never launched. There are no errors in logcat. Because I did it with the wizard, the activity is automatically added to the manifest.

Upvotes: 0

Views: 210

Answers (2)

Asim Roy
Asim Roy

Reputation: 10224

Use startActivity() insteed of startService()

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

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75619

Your code calls startService() but it should startActivity():

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

Upvotes: 3

Related Questions