android - Best practice for updating arguments of a fragment? -
i know "newinstance"-pattern (best practice instantiating new android fragment). how update these arguments of fragment example if fragment changes data?
i know callback-methods between fragments/activitys, these callbacks won't update arguments?!
for example: on creation of fragment pass uri bundle. fragment changes uri via changeuri(uri uri) method callback on first fragment. if fragment gets recreated (for example due screen rotation) use first uri arguments bundle instead of later updated uri, correct?
what best practice solve this? have manually store in savedinstancestate , on usage decide whether use instancestate or arguments-bundle?
i'm looking standard way of handling arguments of fragments, think i'm going such approach (pseudo-code):
private uri arg1; public static fragment newinstance(uri arg1) { create bundle create fragment instance set bundle fragment return fragment } private void oncreate(bundle savedinstance) { if(savedinstance != null) { arg1 = savedinstance.uri } } private uri geturi() { if(arg1 == null) { arg1 = getarguments.uri } if(arg1 == null) { arg1 = defaultvalue } }
so have unified way access argument. , don't have use if-else-hassle, every time need argument.
what think it?
you can't change arguments once set , fragment
added activity
, used similar approach defined yourself. first checked bundle
passed oncreate()
if not null use if null use arguments. , save whatever newest data in onsaveinstancestate()
. more detail : is possible pass arguments fragment after it's been added activity?
Comments
Post a Comment