Start a fragment via Intent within a Fragment
You cannot open new fragments. Fragments need to be always hosted by an activity. If the fragment is in the same activity (eg tabs) then the back key navigation is going to be tricky I am assuming that you want to open a new screen with that fragment.
So you would simply create a new activity and put the new fragment in there. That activity would then react to the intent either explicitly via the activity class or implicitly via intent filter
s.
Using intent to call a fragment from another fragment
If all the fragments are within the same ViewPager and you just want to be navigated to that fragment then you dont need any Intents
. all you need to is :
mViewPager.setCurrentItem(POSITION, true);
here POSITION is an integer.
Now the view pager is in Activity
but the Button
is inside Fragment
so you need to do this create this method inside your activity
public void selectFragment(int position){
mViewPager.setCurrentItem(position, true);
// true is to animate the transaction
}
and call like this inside your OnClickListener
:
((MainActivity)getActivity()).selectFragment(POSITION_YOU_WANNA_SELECT);
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 {
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 to open fragment class through intent in another activity?
SlidingScreenActivity:
@Override
protected void onStart() {
super.onStart();
displayView(getIntent().getIntExtra("position", 2));
}
Another Activity button listener:
btnWallet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mIntent=new Intent(this,SlidingScreenActivity.class);
intent.putExtra("position", 2);
startActivity(mIntent);
}
});
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
Related Topics
How to Use Device Default Theme for App
How to Have a Viewpager Inside of a Scrollview
Android Sdk Cannot Be Found by Flutter
Android - Add Textview to Layout When Button Is Pressed
Is Google's Android Opengl Tutorial Teaching Incorrect Linear Algebra
Fire a Pinch In/Out Command to Android Phone Using Adb
Change Text Color of Selected Item in Spinner
Android - Making Sliding Drawer to Slide from Left-To-Right
Can't Update Android Studio - Only Download
Text/Design Tab Missing New Android Project on Android Studio Ide
How to Get All Child's Data in Firebase Database
How to Change Package Name in Android Studio
Android Studio 2.0 - Pause/White Screen on App First Run
How to Save the Image to Sd Card on Button Click Android