How to Save / Restore nested fragments in android? - android

The layout file of my main activity contains a in wich I'm loading
fragments dinamically using NavigationDrawer.
To achive this I use a FragmentPagerAdapter:
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
In FragmentA there is a ViewPager + another FragmentPagerAdapter, so you can swipe
between three other fragments. (Frament1, Fragment2, Fragment3).
It works like this:
(I can't insert the image due to the lack of reputation...)
http://62.165.232.86:1991/images/fragments.png
When I swap between Frament1, Fragment2 and Fragment3, it works fine.
(I set the ViewPager's offScreenPageLimit high enough.)
The problem occures when I navigate to FragmentB or FragmentC and then FragmentA gets destroyed and I dont know how to save its instance and recover it, becaues I dont have
reference to Fragment1.
EDIT 1:
I swap between FragmentA and FragmentB like this:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, mAdapter.getItem(0));
tx.commit();
Im a bit confused. I should save the instances of Fragment1, 2, 3 and Fragment A,
and recover them all when I navigate back to Fragment A but I don't know how because I dont have reference to the child fragments.
Can you help me?
Thanks for your answers,
Daneel Olivaw

Idea (I not sure at 100%):
Fragment fragment = getFragmentManager().findFragmentByTag("yourtag");
if (fragment==null) {
fragment = mAdapter.getItem(0)
}
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.add(R.id.main, fragment, "yourtag");
tx.commit();

Related

Nested fragment not getting removed completely

In my activity, I have various fragments. By default activity displays a map. On listitem click, Fragment A, B or C is displayed using following code:
protected void replaceFragment(int i) {
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
switch (i) {
case FRAGMENT_A:
aFragment = new AFragment ();
fragmentTransaction.replace(R.id.main_framelayout_replace,
aFragment , TAG_A_FRAGMENT);
fragmentTransaction.commit();
break;//and so on.....
default:
break;
}
}
Here I'm facing an issue: When I replace Fragment A with Fragment B which is nested fragment i.e. it contains list and detail fragment within itself. When I try to remove any fragment other than Fragment B, I'm able to do it successfully and show the default map screen but when I'm on Frgament B and try to remove it, I'm not able to see default map screen. Instead a blank white screen is getting displayed.
Removing of fragments is done as follows:
if (aFragment != null) {
fragmentManager.beginTransaction()
.remove(aFragment ).commit();
}//and so on...
For fragment B which is having list and detailed fragment, I also do following in onDetach of fragment B,
fragmentManager.beginTransaction()
.remove(MainActivity.listFragment).commit();
Note: I'm not getting exception in any try catch. All the lines of code are getting executed without error including onDetach of Fragment B.
Am I doing anything wrong here? Any help appreciated.

Unable to remove Nested fragment completely from Main Fragment

In my activity, I have various fragments. By default activity displays a map. On listitem click, fragment A, B or C is displayed using following code:
protected void replaceFragment(int i) {
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
switch (i) {
case FRAGMENT_A:
aFragment = new AFragment ();
fragmentTransaction.replace(R.id.main_framelayout_replace,
aFragment , TAG_A_FRAGMENT);
fragmentTransaction.commit();
break;//and so on.....
default:
break;
}
}
Here I'm facing an issue: When I replace FragA with FragB which is nested fragment i.e. it contains list and detail fragment within itself. When I try to remove any fragment other than FragB, I'm able to do it successfully and show the default map screen but when I'm on FragB and try to remove it, I'm not able to see default map screen. Instead a blank white screen is getting displayed.
Removing of fragments is done as follows:
if (aFragment != null) {
fragmentManager.beginTransaction()
.remove(aFragment ).commit();
}//and so on...
For FragB which is having list and detailed fragment, I also do following in onDetach of FragB,
fragmentManager.beginTransaction()
.remove(MainActivity.listFragment).commit();
Am I doing anything wrong here? Any help appreciated.
Note: I'm not getting any exception in any try catch. All the lines of code are getting executed without error including onDetach of FragB.
In your FragB class (which extends Fragment) you should be using ChildFragmentManager to manage nested fragments.

how to reloading fragment after transaction

I am using swipe view with ViewPager to display tabs in project.
I am adding three tabs(fragments) to AlertTabsPagerAdapter using
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new FragmentA();
case 1:
// Games fragment activity
return new FragmentB();
case 2:
// Movies fragment activity
return new FragmentC();
}
return null;
}
I am having two problems:
By default OffscreenPageLimit of ViewPager is two, so in my case for first time FragmentA and FragmentB will get created. I want to reload or recreate FragmentB on swipe from FragmentA. How can I achieve this?
In the first tab (i.e. FragmentA) I am having multiple fragments like FragmentA through FragmentD. For this I am replacing FragmentA with FragmentD using FragmentTransaction.
After the fragment transaction onback key press from FragmentD i am getting back to FragmentA, but now I want to reload or recreate FragmentA.

Fragment transaction on a single tab of ViewPager in SherlockFragmentActivity

I am creating ViewPager in SherlockFragmentActivity.
Structure of my app is like ViewPager->having three tabs: tab1, tab2, tab3.
In tab2, I want to display different fragments like fragment1->fragment2->fragment3.
In ViewPager, I am having three tabs (Fragments) added using FragmentStatePageAdapter as shown below.
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
SearchTab searchtab = new Tab1();
return tab1;
case 1:
BrowseTab browsetab = new Tab2();
return tab2;
case 2:
SavedItemsTab savedTab = new Tab3();
return Tab3;
}
return null;
}
Now in tab2, I have displayed ListView in fragment1. When user clicks an item from ListView, I want to display another fragment (fragment2) with other ListView. I am doing this by fragment transaction as bellow:
Fragment newFragment = new Brxxxx_Fragment();
Bundle args = new Bundle();
args.putString("Main_Cat_ID", Main_Category_ID_Str);
args.putString("Main_Cat_Name", Main_Category_Name_Str);
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.b_p_layoutid, newFragment);
transaction.addToBackStack(null);
transaction.commit();
After this step, I am able to display another fragment (fragment2), but my old fragment (fragment1) is still visible below the new fragment.
If I want to remove previous fragment without using replace(), how should I get the current fragment to pass in remove method?
Maybe you can remove the other fragment if you don't need to navigate back or set background color to the new fragment . You can check that your layout id 'R.id.b_p_layoutid' is of your 'FrameLayout' and not your 'Fragment' id just in case...
android:background="?android:attr/colorBackground"
getActivity().getSupportFragmentManager().beginTransaction().remove('YOUR_FRAGMENT').commit();
You can check this answer too:
Visit Android replace fragment still displays some of the replaced fragment
Visit Fragment replaced still visible on background

how to save the state of a fragment for further use (and to avoid reconstruction)?

i'm a beginner on android development and the problem really troubles me a lot
now i have code lines like this:
getFragmentManager()
.beginTransaction()
.replace(R.id.activity_blank, new WordlistPage(unit, false), "wordlistitems")
.commit();
each time the above section is executed, a new WordlistPage is created and the fragment view is initialized.
for example, currently i am reading the content in fragmentA, and after a while i switch to fragmentB and in one minute i return back to fragmentA. what i want is that the fragmentA be the same as it was and not be reconstructed.
so any suggestions?
To avoid to create a new fragment each time I switch fragments I've done somethig like this:
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag("FragmentTag");
if (fragment == null) {
fragment = new CustomFragment();
fragmentManager.beginTransaction().replace(R.id.frag, fragment, "FragmentTag").commit();
} else {
fragmentManager.beginTransaction().attach(fragment).commit();
}

Categories

Resources