Sunday 15 February 2015

android - Check if Fragment exists in Fragment TabHost Activity -



android - Check if Fragment exists in Fragment TabHost Activity -

i have activity a, implements fragment tab host 2 tabs. first tab list view , sec tab has same info in expandable list view. below code snippet fragment activity:

mtabhost = (fragmenttabhost) findviewbyid(android.r.id.tabhost); mtabhost.setup(this, getsupportfragmentmanager(), r.layout.activity_list); mtabhost.addtab( mtabhost.newtabspec("tab1").setindicator("list"), listfragment.class, null); mtabhost.addtab( mtabhost.newtabspec("tab2").setindicator("exlist"), expandablelistfragment.class, arg2);

now issue if phone call activity 1 time again creating fragments again. need check if fragment exists need not add together new one. tried below stuff isn't working

fragment f = getsupportfragmentmanager().findfragmentbytag("list"); if (f!=null && f instanceof listfragment){ log.i("listactivity","already created"); }

for above code getting null fragment f when activity called again. read similar posts nil seem working. please advise how can create sure fragments not created 1 time again when listactivity called. thanks.

i don't think should matter maintain instance of fragment live in tabactivity. if activity not destroyed, won't fragments be. if activity destroyed, , fragments too, it's not big problem recreated, old instance cleaned garbage collector. don't understand why you'd want command behavior. initialize tabbar in oncreate() of activity, destroy in ondestroy() , allow android lifecycle it's job.

android android-fragments

No comments:

Post a Comment