In My application I have 5 tabs.
In 1st tab when user clicks a button I need to go to 2nd tab which contain 3 fragments.
1) List Fragment
2) Details Fragment and
3) Result Fragment
When user click on a button I want to go directly to the Details Fragment, and when clicks back it should go to the list.
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();
In your code instead of calling getActivity().getSupportFragmentManager() call getActivity().getchildfragmentmanager()
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getChildFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();
Related
Hi I have a navigation drawer and I have 4 fragments. I want to go from fragment 3 to fragment 2 on back press and on back press from fragment 4 it should go to fragment 1.How can I do this ??
I have tried adding fragments to back stack. If I add fragment to back stack when I click from home fragment the app should actually close.Instead of closing the fragment which I added to backstack is showing and when back pressing from that it showing the home fragment again and then the app is closing.
//try this code add fragment number while opening new fragment and remove last positions from the list on backpress
public ArrayList<Integer> list = new ArrayList<>();
public void addToStack(int pos)
{
if (!list.contains(pos))
{
list.add(pos);
}
}
According to I have an Activity which contains ViewPager which loads three fragments:
RootA
RootB
RootC
RootA loads another fragment which named as FragA.
RootB loads another fragment which named as FragB.
RootC loads another fragment which named as FragC.
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.container, new FragA());
fragTransaction.commit();
FragA has a button named as ButtonA to load FragAA.
FragB has a button named as ButtonB to load FragBB.
FragC has a button named as ButtonC to load FragCC.
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
fragTransaction.replace(R.id.container, new FragAA, "FragAA");
fragTransaction.addToBackStack("FragAA");
fragTransaction.commit();
I run my App and in the first tab, I press ButtonA. FragAA will be shown. Now I swipe to the second tab and press ButtonB and go to FragBB and then I swipe to the third tab and press ButtonC and go to FragCC.
After that, I swipe to the second tab and then I swipe to the first tab again without any clicking. then I press Back Button. By the first back pressing FragCC will be closed (will put out of BackStack), by the second back pressing FragBB will be closed, and by the third back pressing FragAA will be closed.
But I want my app to close child of current tab not to close fragments according to the BackStack.
I mean the Back Button should close FragAA because I am in the first tab and I pressed Back Button in the first tab!
How can I do it?
I have 3 fragments Fragment A is the main fragment and Fragment B is a child fragment. When clicking on fragment B , I want fragment C to replace the whole fragment A and navigate to it again when button back is pressed from C.
The problem that i can't solve is that the framelayout container is in Fragment A xml so what fragment container i should give to fragment C :
Fragment fragment = new PetDetailFragment();
String fragmentTag = fragment.getClass().getName();
getFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment, fragmentTag).commit();
when the user clicks on fragment B just execute
getFragmentManager().beginTransaction().add(R.id.frameLayout, new FragmentC(), fragmentCTag).commit();
and then when the user presses back fragment A will be visible automatically. To display fragment A manually execute
getfragmentmanager().popbackstack()
I need to create navigation of fragments like in Gmail app. It's like: we have one main fragment A, we can open another fragment (B,C,D...) from navigation drawer, and when we open new fragment, it`s open on top of main fragment, and when press back button, in all cases we come back to main fragment A, don't depend of count new opened fragments. It's seems, first main fragment A we use add method(int FragmentTransaction) without adding to fragment backStack. Then, next fragment B we use method add too, with adding to back stack. And when I need to open another one (Fragment C), I need to replace second fragment B. But, when I use method replace(), replaced all container, and main fragment A not showing when back button pressed from fragment C or B and app close. So, the question is: how to replace only fragment B or C, without losing fragment A?
A valid solution would be to have two container framelayouts in your activity. The first one (which will below the other one) contains your fragment A. Everything you open will be added/replaced in the second container.
Another solution is to include the fragment A statically in your layout and have your container framelayout on top of it where you add your fragments B, C, D etc.
open fragment Like this
HighlightFragment highlightFragment=new HighlightFragment(FirstReaderScreen.this); //Your fragment
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, highlightFragment) // LL_fragment is container
.addToBackStack(null)
.commit();
and in Activity OnBackPress
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
I have a fragment and an activity.there are some fields and an button in fragment.When i click an button in fragment then i want to go to an activity.In that activity i have another button.when i click on that button i want to return from the present activity to the previous fragment.Every thing works fine.what i need is i want to recreate the fragment when i return to the fragment from the activity..i.e,for example in the fragment if i enter anything in the edit_text before passing to the activity, which is also presented when i returned from the activity.(the fragment is same as how i left from fragment) which is i don't want .i want a fresh page.i want to recreate a fragment when i returned from activity ..i need to select fields newly..can any one suggest any help..thanks in advance..
Try to use main FragmentActivity and Fragments and organize transactions between fragments. I didn't meet examples with Activity and Fragment in one Addlication.