android - FragmentTransaction.replace() fade-in transition shows "ghost" fragment -
you can download entire project try , debug. here repo of entire code: https://bitbucket.org/lexic92/studio48/
i have "ghost fragment" appearing in transition when try replace blank fragment blank fragment.
how recreate problem: have navigation drawer , when click on item, opens fragment fills whole screen.
navigation drawer:
- button 1 - fragment text
button 2 - fragment empty
when open app, starts out fragment text. then, open navigation drawer , click button 2. transition bad, , turns blank screen split second, switches fading text, until becomes blank screen.
if open navigation drawer , click on button 2 again, fades full text blank screen. so, for 1 split-second, shows fragment text when not supposed to. ran through app on debug mode, step step, slow down , verify behavior happened.
if open navigation drawer , click on button 2 again third time, correctly not show animations because fading same screen.
does know why happens? think has navigation drawer?
here repo of entire code: https://bitbucket.org/lexic92/studio48/
here code excerpts:
songdetailactivity.java:
@override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments fragmentmanager fragmentmanager = getsupportfragmentmanager(); //open blank screen if(position == 1) { fragmentmanager.begintransaction() //.setcustomanimations(r.anim.fade_in, r.anim.fade_out) .settransition(fragmenttransaction.transit_fragment_fade) .replace(r.id.songlist_detail_container, placeholderfragment.newinstance(position + 1, msongid)) .commit(); } //open screen text else { songdetailfragment sdf = new songdetailfragment(); bundle args = new bundle(); args.putstring(songdetailfragment.arg_item_id, msongid); sdf.setarguments(args); fragmentmanager.begintransaction() //.setcustomanimations(r.anim.fade_in, r.anim.fade_out) .settransition(fragmenttransaction.transit_fragment_fade) .replace(r.id.songlist_detail_container, sdf) .commit(); } }
the same problem happens when comment out 2 lines ".settransition" , un-comment 2 lines ".setcustomanimations". here custom animations:
res/anim/fade_out.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromalpha="1.0" android:toalpha="0.0" android:duration="@android:integer/config_mediumanimtime" /> </set>
res/anim/fade_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromalpha="0.0" android:toalpha="1.0" android:duration="@android:integer/config_mediumanimtime" /> </set>
how can transition smoothly without "ghost" fragment showing up?
specs: running android.support.v7.app.actionbaractivity, minsdkversion == 15, , targetsdkversion == 21, on google nexus 5 physical device (not emulator).
my guesses of answer:
this website seems give me hint: http://gotoanswer.com/?q=android+animation+within+a+fragment+not+working
personally me bug zposition fragment animations.
what see new fragment "appearing" because new fragment attached underneath current one.
so when trying animate fragment 'over top' of existing fragment, appears nothing happening new 1 'appears'.
i have tried many work arounds, playing zposition (only affects windows not layouts, no effect fragments), onanimationcreate() bringing root layout front or on animation start , stop... seems nothing.
so @ point in time without writing complete custom animation fragment transitions, bit buggy @ moment.
you may have play with. .add(fragment) wait added, call .remove(fragment) removing old fragment, way new fragment physically placed on top of existing fragment.
i did notice this code being called upon instantiation of songdetailactivity.java, without me clicking on navigation drawer:
//open screen text else { songdetailfragment sdf = new songdetailfragment(); bundle args = new bundle(); args.putstring(songdetailfragment.arg_item_id, msongid); sdf.setarguments(args); fragmentmanager.begintransaction() //.setcustomanimations(r.anim.fade_in, r.anim.fade_out) .settransition(fragmenttransaction.transit_fragment_fade) .replace(r.id.songlist_detail_container, sdf) .commit(); }
maybe current fragment fading out "underlying" fragment, quotation suggests, before fading in new fragment?
what ended doing
note: not putting answer because not technically answer question, it's best back-up-plan solution can come with.
i tried seeing if problem solved when there no animation. "ghost fragment" didn't show up, did notice took split second load fragment, looked jarring , ugly. wanted silky smooth app, , if isn't smooth when there no animation, hopeless if did fix ghost fragment bug. learned fragments buggy (see here).
i decided hide , show views instead. here new code:
songdetailactivity.java
//---------- navigation drawer ------------- @override public void onnavigationdraweritemselected(int position) { //home if (position == 0) { } //lyrics else if(position == 1) { findviewbyid(r.id.lyrics).setvisibility(view.visible); findviewbyid(r.id.info).setvisibility(view.invisible); } //info else if(position == 2) { findviewbyid(r.id.info).setvisibility(view.visible); findviewbyid(r.id.lyrics).setvisibility(view.invisible); } }
i copied , pasted layouts 2 fragments (the empty 1 , 1 lots of text) same layout, children of framelayout parent:
fragment_songlist_detail.xml:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:background="#f8bbd0" android:layout_width="match_parent" > <linearlayout android:id="@+id/lyrics" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> ... </linearlayout> <linearlayout android:id="@+id/info" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> ... </linearlayout> </framelayout>
it works fine, don't know how make fade in , fade out. question separate stack overflow question. if solves ghost fragment mystery, i'm interested know answer. decided i'm not going waste time trying figure out right now, when fragments' transitions have proven buggy in past (like in article).
do transaction calls
fragmentmanager.begintransaction().settransition(fragmenttransaction. transit_fragment_fade).remove(current_fragment).commit(); fragmentmanager.begintransaction().add(r.id.songlist_detail_container, placeholderfragment. newinstance(position + 1, msongid)).commit();
see if helps. idea first commit remove,after commit addition. can go fragmentmanager.hide(fragment);
Comments
Post a Comment