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);
startActivity(intent);

From an Activity

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

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 FavoriteActivity.java if you choose to do this since Java requires class names match the file name.

UPDATE

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);
startActivity(intent);

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(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AnotherActivity.class);
startActivity(intent);
}
});

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(), Main::class.java)
getActivity().startActivity(intent)

or maybe something like

activity?.let{
val intent = Intent (it, Main::class.java)
it.startActivity(intent)
}

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 {
activity?.let{
val intent = Intent (it, Main::class.java)
it.startActivity(intent)
}

}
}

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();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();

And to replace fragment do this:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();

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(R.id.tab1upgradeCost);

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

Instead use

Button tab1=(Button) rooView.findViewById(R.id.tab1upgradeCost);


Related Topics



Leave a reply



Submit