android - onViewCreated - wrong place to replace fragment? -
i'm showing empty fragment if user has no data. in emptyfragment (in onviewcreated
) check condition , replace empty fragment one, call method on according activity replaced fragment.
some users (currently samsung, 6.0.1 don't know if means anything) experience crash on line executependingtransactions
:
illegalstateexception: fatal exception: java.lang.runtimeexception unable resume activity {....app.activities.myactivity_}: java.lang.illegalstateexception: fragmentmanager executing transactions
is bug in 6.0.1 or onviewcreated
wrong place this?
edit
would possible solution?
if (addtobackstack) { getsupportfragmentmanager().begintransaction().replace(r.id.fragment_container, contentfragment, name).addtobackstack(name).commitallowingstateloss(); getsupportfragmentmanager().executependingtransactions(); } else { getsupportfragmentmanager().begintransaction().replace(r.id.fragment_container, contentfragment, name).commitnow(); }
first of all: why don't move 'check condition' in activity? in way, can decide fragment need: 1 manages empty values or other one.
second: suggest read 2 small tutorials fragments commit , state loss. can find many error conditions related fragments there.
and answer related 'fragmentmanager executing transactions': there @ least 2 nested calls executependingtransactions()
method, means you're asking fragment manager (there single instance of fragment manager per activity) execute transaction while it's executing transaction. it's hard identify issue, should post code of activity , involved fragments, identify , remove 1 (the first one) call executependingtransactions, can give tips:
- do not use
executependingtransactions()
. it's hard mantain code requires sort of orchestration between fragments - use
commit()
if don't need immediate access fragments you're adding activity. simple commit execute transactions main thread free. - if need immediate access fragment, use
commitnow()
. i've used method few times, correctly handle dialogfragments.
commitnow()
available pre 24 devices if use support library, suggest adopt.
Comments
Post a Comment