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)
}
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
Intent does not start Activity from Fragment
As the starting of a new Activity from within the Fragment doesn't work, you could try migrating the code to the hosting Activity. A clean and reusable way is to create an Interface in a new file.
public interface myInterface {
public void startMyIntent(Intent i);
}
Then, implement this Interface in the Activity class, hosting your Fragment
public class hostingActivity extends AppCompatActivity implements myInterface {
@Override
public void startMyIntent(Intent i) {
startActivity(i);
}
}
In your Fragment's onItemClickListener
, you can call it like this
_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Activity parentActivity = getActivity();
Intent in = new Intent(parentActivity, DealView.class);
in.putExtra("deal", position);
in.putExtra("city", _citySearchBar.getText());
((myInterface)parentActivity).startMyIntent(in);
Log.d("BASE_FRAGMENT", "Activity should have been started here");
}
});
}
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
How to Keep the Spaces at the End And/Or at the Beginning of a String
Android: Remove All the Previous Activities from the Back Stack
Is 'Shouldoverrideurlloading' Really Deprecated? What How to Use Instead
How to Set Android_Sdk_Home Environment Variable
Flutter Projects & Android X Migration Issues
Why Mediaplayer Throws Not Present Error When Creating Instance of It
Android - Acceleration Down (Smash)
Full Screen Background Image in an Activity
How to Get Unique Device Hardware Id in Android
Bluetooth and Wifi Printing for Android
How to Get Color-Int from Color Resource
Start an Activity from a Fragment
Edittext Maxlines Not Working - User Can Still Input More Lines Than Set
Is There a Simple Example of the Popupwindow Class Using Android V2.0
Detect Touch Event on a View When Dragged Over from Other View
Android:Maximum Allowed Width & Height of Bitmap