Replace a fragment programmatically - android

I have three fragments as shown in below figure. I have added all these three fragments in LinearLayout using .xml file and when my launcher activity starts I load that .xml layout using setContentView.I have some controls on fragment2. Clicking on any one loads the
fragment4 programmatically using FragmentTransaction and commit method. This fragments is added to the screen but the problem is the prgrammatically added fragment4 take the whole screen area. What can be the problem?Note: On any item click at f2 I want to replace only f2 with new fragment f4. Keep in mind I have added f1, f2, f3 through xml layout file and adding new fragment f4 programmatically.

You should always add, remove and replace your fragments programatically. As such I suggest you replace your F-1, F-2 and F-3 fragments with containers such as FrameLayout.
Basically instead of having a <fragment/> element as F-1 you make it a <FrameLayout/> element. Then you perform a fragment transaction in your FragmentActivity's onCreate:
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.f1_container, f1); // f1_container is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
Now, Suppose you have done this for F-1, F-2 and F-3. You then replace f2 with f4 by doing the same thing again in your OnClickListener:
public void onClick(View v) {
Fragment4 f4 = new Fragment4();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.f2_container, f4); // f2_container is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}

Keep only FrameLayout as placeholders for the fragment in the XML. In the OnCreate load the fragments in the framelayout. OnClick of the fragment, give that particular FrameLayout's id to replace by Fragment4.

Related

Android: Replace sub fragment with it's parent fragment in main activity container

Imagine i have main activity that has viewpager. I have 2 fragments called (F1 & F2) that will transaction into viewpager.
Again imagine in F1 fragment i want to set a button. When clicking on button, i want to transaction other fragment call SUBF1 but not into F1 fragment.
My question is here!!! Is it possible to replace SUBF1 with it's parent means F1?My idea is that i want to replace sub fragment with it's parent fragment that has been kept on fragment's container in main activity?
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
You can save the instance of current fragment, when you are
navigating from one fragment to other. When user press the back
button, you can open the specific fragment with the help of tag.
#Override
public void onBackPressed() {
String currentTag = currentFragment.getTag();
if(currentTag.equals(res.getString(R.string.fragmentTagTest)))
{
currentFragment = AnotherFragment;
replaceFragment() // integrate replace current fragment with new one.
}
}

Added fragment scrolled down

I have two fragments the first fragment contains list of linear layouts and the whole fragment is in scroll view the second fragment is added and the first is hidden on choosing item from the first.
The problem is the second fragment is created scrolled down if the first fragment was scrolled down.
I tried the ways to force second fragment to be scrolled to (0,0) but failed.
the code used to add the second fragment
public void setActionOnClick(String id) {
CommentFragment frag = new CommentFragment();
Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("TAG", TAG_NEWS_STORY);
((MainActivity) getActivity()).setCurrentTag(TAG_NEWS_STORY);
frag.setArguments(bundle);
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.add(R.id.main_content, frag, TAG_NEWS_STORY);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
on Attach of second fragment the first fragment is hidden.
I don't want to use fragmentTransaction.replace because there are calls to the api which I don't want to reload.
this is an old question but. probably your fragment place_hoder is inside an scrollview.
just check if R.id.main_content is in a scrollview or not.
you should add the ScrollView to the fragment-layout instead of instantiating the fragment inside one
Before this:
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
Add code to remove old fragment:
getSupportFragmentManager().beginTransaction().remove(yourOldFragment).commitAllowingStateLoss();

Remove single fragment from backstack Android

I am struggling to remove a single fragment that I add dynamically when I have multiple fragments.
For examples:
MainActivty
Inflate FragA
Inflate FragB
Inflate FragC
Now how do I just delete fragment A?
Using popBackStack kills all three and getSupportFragmentManager().beginTransaction().remove(TAG).commit(); also seems to do thae same thing
How are you meant to do this correctly? I am trying to keep multiple backstacks persistent over tabs
try this...
get current fragment title & check fragment A or not.
get current fragment title to use getTitle() Method and check
if(getTitle().toString.equals(fragment A){
// do
}else{
// do
}
Can you try this..
For eg. using fragment name as tag:
FragmentA fragment = new FragmentA();
String backStateName = fragment.getClass().getName();
Adding to backstack:
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
Popping:
getSupportFragmentManager().popBackStackImmediate (backStateName, 0);
This should only pop the fragment with the specific tag.

Android remove fragment by container

Currently, I am working on a new android project.
In my project, I have 2 FrameLayout (called F1, F2) which are used as containers of Fragment.
On "F1" I will add a fragment (Like navigation drawer fragment, menu item....).
On "F2" I called its MainContainer - It's will use container all "Main" fragment. (I mean F2 will container fragment like HomeFragment, GameFragment......).
Here are some steps:
F1 -> add fragment f1;
F1 -> add fragment f2;
F2 -> add fragment g1;
F2 -> add fragment g2;
F1 -> add fragment f3;
Now I want to use the transaction fragment on Container F2.
I mean:
I can add/remove a fragment on Container F2. But in android Fragment Manager will manager all Fragment f1..f3, g1..g2;
So I called popBackStack -> It's will remove fragment f3. (But I want to remove g2).
For any task like addFragment, remove, replace fragment I only want to do Fragment in Container F2. (F1 will be never changed).
Please help!
When you adding the fragment, you can use addToBackStack(String name) and disallowAddToBackStack() to control the back stack of FragmentManager.
For your requirement, you should use the code like this to add f1, f2, and f3:
getFragmentManager().beginTransaction()
.add(R.id.F1, [your fragment instance])
.disallowAddToBackStack()
.commit();
And use the code like below to add g1 and g2:
getFragmentManager().beginTransaction()
.add(R.id.F2, [your fragment instance])
.addToBackStack(null)
.commit();
Check the officail document of addToBackStack(String name) and disallowAddToBackStack() for more details.
You should use child fragment when adding child to that fragment.
public void addFragB() {
FragmentManager childFragMan = getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
FragmentClassB fragB = new FragmentClassB();
childFragTrans.add(R.id.fragA_LinearLayout, fragB);
childFragTrans.addToBackStack("B");
childFragTrans.commit();
}
And when removing the fragment
public void backPressed() {
int childCount = parentFragment.getChildFragmentManager().getBackStackEntryCount();
if (childCount == 0) {
// it has no child Fragment
} else {
// get the child Fragment
FragmentManager childFragmentManager = parentFragment.getChildFragmentManager();
// removing the child Fragment from stack
childFragmentManager.popBackStackImmediate();
}
}

how to call fragment having no layout

I have 2 fragments FileManage and another is FragmentA. FileManage has no layout of it's own but FileManage class wants to call FragmentA .so how can be This Possible . i have tried code given below but it is not working
FragmentA f12 =new FragmentA();
android.support.v4.app.FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(0, f12);
transaction.commit();
Normally here in replace function we put 1st Paramtere as layout of base Activity ..but here i have no layout so what to do???

Categories

Resources