Launch New Activity from Fragment in Android Studio

Start an activity from a fragment

mFragmentFavorite in your code is a FragmentActivity which is not the same thing as a Fragment. That's why you're getting the type mismatch. Also, you should never call new on an Activity as that is not the proper way to start one.

If you want to start a new instance of mFragmentFavorite, you can do so via an Intent.

From a Fragment:

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

From an Activity

Intent intent = new Intent(this, mFragmentFavorite.class);

If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent.

Also, I recommend changing your class names to be more accurate. Calling something mFragmentFavorite is improper in that it's not a Fragment at all. Also, class declarations in Java typically start with a capital letter. You'd do well to name your class something like FavoriteActivity to be more accurate and conform to the language conventions. You will also need to rename the file to if you choose to do this since Java requires class names match the file name.


Also, it looks like you actually meant formFragmentFavorite to be a Fragment instead of a FragmentActivity based on your use of onCreateView. If you want mFragmentFavorite to be a Fragment then change the following line of code:

public class mFragmentFavorite extends FragmentActivity{

Make this instead read:

public class mFragmentFavorite extends Fragment {

Start a new Activity from Fragment

If you have a look at the documentation you can see that to start an activity you'll want to use the following code

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

Currently you're using MainActivity.class in a place that requires a context object. If you're currently in an activity, just passing this is enough. A fragment can get the activity via the getActivity() function.

Your full code above should look like this

Button button = (Button) rootView.findViewById(;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AnotherActivity.class);

Launch new activity from fragment in Android Studio

tnx for answers!
The problem was - i didn't declare the SkipActivity in manifest file.

At the moment i did it- the app is running and i can swipe between fragments and launch another Activity from fragment.

tnx a lot!

Kotlin: open new Activity inside of a Fragment

Because Fragment is NOT of Context type, you'll need to call the parent Activity:

 val intent = Intent (getActivity(),

or maybe something like

val intent = Intent (it,

Kotlin: how to open activity from fragment with button in android

Make your button's listener after your fragment view is created, which gives callback in onViewCreated

onCreateView is called when Fragment's view is in the process of creating and you are accessing your fragment view's child before creating it.

It should be done like,

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

button25.setOnClickListener {
val intent = Intent (it,


How to start Fragment from an Activity

You can either add or replace fragment in your activity. Create a FrameLayout in activity layout xml file.

Then do this in your activity to add fragment:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();

And to replace fragment do this:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();

See Android documentation on adding a fragment to an activity or following related questions on SO:

Difference between add(), replace(), and addToBackStack()

Basic difference between add() and replace() method of Fragment

Difference between add() & replace() with Fragment's lifecycle

How to open Activity from Fragment using intent?

Your getting NLP because tab1 is not initialized with right view

Don't use

Button tab1=(Button) getView().findViewById(;

In onCreateView() getView() will retrun null because still fragment is not initlized.

Instead use

Button tab1=(Button) rooView.findViewById(;

Related Topics

Leave a reply