If I may ask: Why are you createing all these fragments and then hiding them? Why not just create a fragment when you need it and replace the existing one?
On Wed, Apr 25, 2012 at 12:09, mosimo <[email protected]> wrote: > I've been playing with this more today and have got it working but it seems > like a bit of a bodge job. So if anyway has a better more efficient way of > doing this I'd appreciate it. > > -------Working code for newFrags()---- > private void newFrags() > { > int i = 0; > foreach (String s in countries) > { > FragmentTransaction ft = > this.FragmentManager.BeginTransaction(); > MyFragment fragment = new MyFragment(s); > ft.Add(Resource.Id.frag_container, fragment, s); > if (i < (countries.Length - 1)) > { ft.Hide(fragment); } > ft.Commit(); > i++; > currFrag = s; > } } > > > > -------------ORIGNAL POST CODE---------- > ----Partial code of Activity1.cs ------------ > Private String currFrag = ""; > private FragmentManager fm; //fm = this.FragmentManager; in OnCreate > > static readonly string[] countries = new String[] { > "Afghanistan","Albania","Algeria","American Samoa","Andorra", > "Angola","Anguilla","Antarctica","Antigua and > Barbuda","Argentina", > "Armenia","Aruba","Australia","Austria","Azerbaijan" } > > > //Make new fragments with replace > //This works visually but I cannot access the fragments by their tags > //unless they are the current fragment being shown in frag_container > private void newFrags() > { > foreach (String s in countries) > { > FragmentTransaction ft = this.FragmentManager.BeginTransaction(); > MyFragment fragment = new MyFragment(s); > > //Replace fragment in container with new fragment MyFragment and > give it tag s > ft.Replace(Resource.Id.frag_container, fragment,s); > ft.Commit(); > //Set String currFrag to the tag of the newly set Fragment > currFrag = s; > } > > } > > //Make new frags using add and hiding the last made one (if available) > //This is how I would like to do it so it loads all needed fragments > //then hides them as the next one is made > private void newFrags() > { > FragmentTransaction ft = this.FragmentManager.BeginTransaction(); > foreach (String s in countries) > { > //Check if there was a Fragment already created and hide, if not no > need to hide anything > if (currFrag != "") > { > Fragment f = (Fragment)fm.FindFragmentByTag(currFrag); > ft.Hide(f); > } > > MyFragment fragment = new MyFragment(s); > ft.Add(Resource.Id.frag_container, fragment, s); > ft.Commit(); > > currFrag = tag; // If I leave this in App crashes when I run this > function. > } > > } > > > //How I want to switch fragments. This works and keeps all the previous > contents of the fragments > //and allows me to still change things on the fragments in the background > wtihout them > //needing to be visible by finding them using their tags. > private void switchFrag(string tag) > { > FragmentTransaction ft = this.FragmentManager.BeginTransaction(); > ft.Hide(fm.FindFragmentByTag(currFrag)); > ft.Show(fm.FindFragmentByTag(tag)); > ft.Commit(); > } > > -- > View this message in context: > http://mono-for-android.1047100.n5.nabble.com/Fragments-problem-tp5664586p5665582.html > Sent from the Mono for Android mailing list archive at Nabble.com. > _______________________________________________ > Monodroid mailing list > [email protected] > > UNSUBSCRIBE INFORMATION: > http://lists.ximian.com/mailman/listinfo/monodroid -- http://www.opgenorth.net _______________________________________________ Monodroid mailing list [email protected] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid
