I have a button in Fragment. When Button is clicked it has to Open new Fragment/Activity within Fragment. I have written code using Intent,
Intent i = new Intent();
i.setClass(getActivity(), UpdateProfile.class);
startActivity(i);
but its opening in new activity like in below image.
My requirement is in Picture 1. Can someone suggest me how to do it?
EDIT: As suggested by rai and ADK, its working fine but new fragment overlays on old fragment. See the below image. "Change Password"(TextView) is New Fragment which overlays on existing fragment.
Try:
getFragmentManager()
.beginTransaction()
.replace(containerViewId, newFragment)
.addToBackStack(null) // enables back key
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) // if you need transition
.commit();
You should to use FragmentTransaction enter link description here.
Like this
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.yourFragment, YourFragmentWithImageClass.getInstance());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
in your Activity
Related
How to open a Fragment from another Fragment?
I basically have a button inside of my HomeFragment.kt and I want to make it to redirect me to AboutFragment but I'm not sure how to make it. I tried different methods from Stack but nothing worked... Any suggestions? I wanna open the new fragment as I will open from navigation drawer.
Here is the button but my code just work with activities... Not with fragments...
binding.aboutUsBtn.setOnClickListener {
val intent = Intent(activity, AboutFragment::class.java)
startActivity(intent)
}
Try this code plz:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.home_layout, new ChatFragment(), "second fragment"); //My second Fragment
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I have a navigation view that loads some fragments depending on which is pressed. In one of the fragments, I want to have a button, which when it is pressed, the fragment changes from the one loaded by the navigation view to a different fragment. I have tried using
ProfileEditFragment pef = new ProfileEditFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(pef).replace(R.id.drawer_layout, pef, "Fragment")
.addToBackStack(null).commit();
When I do this, it loads the other fragment on top of the current fragment, rather than replace it, even though I used the replace function. How do I fix this?
Can you try this:
ProfileEditFragment pef = new ProfileEditFragment();
FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.drawer_layout, pef);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
I am using navigation drawer and it is simple to use. I am not providing the complete code but providing you detail which could be easy for you to understand my problem. I am using fragments these are about 8 in numbers and I am replacing them with one an other. But here comes a problem
I am replacing them on click event of the navigation drawer. but there are two main problems
After replacement , I can see the previous fragment in the background. does replace method just call the new fragment over it ? if yes then what should I do to old fragment not be visible in the background of my new fragment.
When I click navigation drawer Item , it loads the specific fragment successfully. but keeping in that fragment when I click to that specific item again it loads this fragment again and again. For example if drawer item num 3 opens fragment MyBook , then by clicking item num three 2 or many times would open fragment that much time.
So please some one answer me how to cure my app for such kind of actions which I described above.
I tried like this. Its working fine me
FragmentManager frgmanager = getFragmentManager();
frgmanager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction frgTransaction = frgmanager.beginTransaction();
if(subitem.equalsIgnoreCase("subitem")){
Frag1 frg1 =new Frag1(mCtx);
frgTransaction.replace(R.id.inflate_layout, frg1);
}else if(subitem1.equalsIgnoreCase("subitem1")){
Frag2 frg2 =new Frag2(mCtx);
frgTransaction.replace(R.id.inflate_layout, frg2);
}else{
Frag2 frg3 =new Frag3(mCtx);
frgTransaction.replace(R.id.inflate_layout, frg3);
}
frgTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
frgTransaction.commit();
you can use addtobackstack in fragmentstranstion object.like
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, new AnotherFragment());
transaction.addtoBackStack(null).commit();
Use replace-method of FragmentTransaction instead of add (http://developer.android.com/guide/components/fragments.html#Transactions)
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, new AnotherFragment());
transaction.commit();
To avoid re-instantiating the fragment, keep track of the current open fragment and only do a fragment transaction, if we next-to-be-opened fragment is a different one than the current.
This may achieved like the following:
class MyActivity ... {
private String currentFragment;
private void openNewFragment(Fragment fragment) {
String newFragment = fragment.getClass().getSimpleName();
if (newFragment.equals(currentFragment)){
// new fragment already shown
return;
}
// Fragment transaction etc here:
}
}
Note that this only compares fragments based in their class name. Sometimes this might not be unique, e.g. if there is a DetailFragment class which displays information about an entity. Which entities details to show may depend on intent arguments.
The above code however will then prevent opening DetailFragment for Entity=1 if currently details for Entity=2 are shown. For these scenarios the information about the fragment kept needs to be extended (e.g. storing a Reference or WeakReference to the fragment instance itself).
I have implemented navigation drawer in my app. When I open the app first it shows blank screen. Instead I want it to start fragmentA.
I have tried
Intent i = new Intent(MainActivity.this, fragmentA.class);
startActivity(i);
but this gives me activity not found exception.
So how can I start a fragment inside an activity?
Try:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.yourFrame, fragmentA.getInstance());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
in your activity
Following code am using while am adding new fragment and adding old fragment to backstack but still old fragment in backstack get click ,what is wrong with my code?
getFragmentManager()
.beginTransaction()
.add(R.id.content_frame, new XyzFragment())
.addToBackStack(null)
.commit();
use this, It is worked for me by using always "replace method" instead of "add method". I never used "add"
Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack("Frag");
fragment.setArguments(null);
ft.replace(R.id.content_frame, fragment);
ft.commit();