How to Start an Activity from Within a Fragment

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);
}
});

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)
}

How can I start an Activity from a Fragment?

Set your layout in activity like this in onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_class);
}

dont use onCreateView for activity

Start an Activity form a Fragment

The author explains it. Your CrimeFragment, in its onCreate() method, gets its hosting activity (through getActivity()) and then attempts to get an UUID from the Intent used to start that Activity.

This means that any activity containing your CrimeFragment now has to obey this rule, i.e. its intent should have (in it) an extra defined by the name EXTRA_CRIME_ID. If that activity does not comply, you'll see an exception being thrown in CrimeFragment's onCreate().

Try having this fragment in a new activity created by yourself to see what happens.

Open new activity by Clicking button in a Fragment

getSupportActionBar().hide(); is what's causing the error. You have added it before

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);

So it's trying to execute it before the activity is initialised.

Add it after the above 2 lines and it should work.

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!



Related Topics



Leave a reply



Submit