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???
Related
I have 3 fragments: Home, A and B. Home and A are in mobile navigation menu.
User goes from A to B and then press back button. The issue that if I use getFragmentManager().popBackStack(); onCreateView of fragment A is calling and I have duplicating of content.
But if I use getActivity().onBackPressed(); it goes to Home fragment instead of A.
How to display fragment A by clicking back button without refreshing the view?
Here is how I make transaction from A to B
CertificateItemFragment certificateItemFragment = new CertificateItemFragment(item);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, certificateItemFragment).addToBackStack(null);
fragmentTransaction.commit();
You need to know some premise before using FragmentManager.
in Activity there is FragmentManager and we should call it with getSupportFragmentManager(), getFragmentManager() is deprecated.
in Fragment there are multiple FragmentManagers called ParentFragmentManager and ChildFragmentManager, the final FragmentManager which is deprecated too. And ParentFragmentManager will be same as Activity's FragmentManager
getActivity().onBackPressed() will pull out fragment if any stack exists in Activity's FragmentManager
fragmentManager.popBackStack() will pull out fragment if any stack exists in Activity's or Fragment's FragmentManager rely on who calls
Base on above points
(1) If You want to hold fragments in Activity, you should call getSupportFragmentManager() in Activity, and call getParentManager() in Fragment, then onBackPressed() will pull out the fragment you last add to stack.
(2) If you want to hold fragments in a Fragment and Separated from Activity, you should call getChildFragmentManager() in Fragment, then activity.onBackPressed() will pull out the fragment which in Activity's fragment stack and ignore other fragments in Fragment's fragment stack.
For question will be same as case (1), and if you dont want re-create fragment, you should use add() instead of replace()
in Fragment A
CertificateItemFragment certificateItemFragment = new CertificateItemFragment(item);
FragmentManager fragmentManager = getParentFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav_host_fragment, certificateItemFragment).addToBackStack(CertificateItemFragment.TAG);
fragmentTransaction.commit();
now onBackPressed() will backstack from CertificateItemFragment to Pre-Fragment
In normal case navigation item list menu open fragment which are replace to each other but when we move inside of any item as like in your case move from fragment A to B,
In this case normally we use another activity on which create fragment.
If you do't want to use activity then, you just need to add fragment.
try below code that may help you
CertificateItemFragment certificateItemFragment = new CertificateItemFragment(item);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav_host_fragment, certificateItemFragment).addToBackStack(CertificateItemFragment.TAG);
fragmentTransaction.commit();
ask for helping to do these steps please:
I have on main activity has one Relative layout
<RelativeLayout
android:id="#+id/main_framelayout_small_contain"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
1.when activity starts it replaces RelativeLayout with fragment (f1)
2.(f1) has listview when user click I have to show another fragment (f2)
in f1 place so I use these method in MainActivity
public void set_fragment_subject(String path)
{
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft= fragmentManager.beginTransaction();
Fragment_subjects f1 = new Fragment_subjects(path);
Fragment_season f2 = new Fragment_season();
ft.add(R.id.main_framelayout_small_contain,f1);
ft.hide(f2);
ft.commit();
}
may it looks strange but I do this after search because I want to retain f1's data when press backbutton.
The problem appears When user clicks in f1 , Activity shows f2 but over the f1.
Sorry to take long time ,Any one can help to do what I want correctly,Thanks
Hello #Ghadeer here you can access fragment as tow way one was the normal replace old fragment and another is maintain back stack, But I think you have need tow second way because you want to first fragment data as, so follow the following step for do it,
Please take frame layout instead of Relative layout
<FrameLayout
android:layout_width="match_parent"
android:id="#+id/main_framelayout_small_contain"
android:layout_height="match_parent"></FrameLayout>
Here I have created tow method we will use in our code so put in your activity where you start your fragment :
public void setFragment(Fragment fragment, int container) {
getSupportFragmentManager().beginTransaction().add(container, fragment).addToBackStack(fragment.getClass().getName()).commit();
}
public void popAllFragments() {
// TODO Auto-generated method stub
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
Please add following code when you start first fragment :
popAllFragments();
Fragment_subjects f1 =new Fragment_subjects();
setFragment(f1,R.id.main_framelayout_small_contain);
And add following code when you start second framgnet :
Fragment_season f2 =new Fragment_season();
setFragment(f1,R.id.main_framelayout_small_contain);
Then after finally on your back button write following code :
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
getActivity().onBackPressed();
Try this may be work for you
You need to replace the current fragment with a new fragment.
This can be done by following code.
Fragment fragmentSeason = new Fragment_season();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.main_framelayout_small_contain, fragmentSeason);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
On back press, you would get your first fragment.
There is some implementations issue in method set_fragment_subject
If you have declare set_fragment_subject method in Fragment_1 then your code in method should be like this,
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft= fragmentManager.beginTransaction();
final Fragment_season f2 = new Fragment_season();
ft.add(R.id.main_framelayout_small_contain,f2);
ft.hide(this);// Here this means the current fragment because this method is declared in Fragment_1
ft.addToBackStack(null);
ft.commit();
How to change the whole view of a fragment with other fragment !!
Or how to close the current fragment with another fragment, please explain with layout also
Thanks in advance...
You can either add or replace fragments in your activity. Create a FrameLayout in activity's layout xml file.
Then do this in your activity to replace fragment. You can use same code each time you want to replace one fragment with other.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
If you want to add fragment instead of replace then 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();
When you want to replace added frogment with anu other fragment then you have to remove previous fragment first (or you can hide previous fragment; depends on your requirement). See following code:
Fragment fragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_STRING_TAG);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
See 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
Or see my answer to a similar question:
How to start Fragment from an Activity
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentlayout,new fragment()).commit()
This will help you replace your existing fragment in view with id FragmentLayout with a new fragment().
ThankYou i hope this was helpful.
First you take one Framelayout in Your Activity where you add fragment.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.framelayout,fragment_1);
transaction.addToBackStack(null);
transaction.commit();
When you replace first fragment with second one you write, just change fragment_1 to fragment_2
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.framelayout,fragment_2);
transaction.addToBackStack(null);
transaction.commit();
Android: I have a list Fragment on an activity.based on the choice, another fragment will be shown on the same activity
but the other fragment should be replaced and i don't know how to do that when the list fragment is fixed !
From google guide for replacing fragments : http://developer.android.com/training/basics/fragments/fragment-ui.html
// Create fragment and give it an argument specifying the article it should show
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
There's a guide on Fragments in the developers portal. Take a look at the "programmatically add" part (and at the whole guide actually). In short: you need a ViewGroup that is used as a container for the fragments and a FragmentTransaction that is used to add/replace fragment in this container.
Something like (taken from the guide):
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);//fragment_container is the ID of the ViewGroup container in your layout
fragmentTransaction.commit();
in your activity.
EDIT:
Long story short - don't put a fixed fragment in your activity's layout. Instead place a container, dynamically add your first fragment in the container and replace it with another fragment when you need to do so (by using FragmentTransaction's replace).
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.