I know about the "newInstance"-Pattern (Best practice for instantiating a new Android Fragment). But how do I update these arguments of a fragment for example if another fragment changes data?
I know about callback-methods between Fragments/Activitys, but these callbacks won't update the arguments?!
For example: on creation of the fragment I pass an URI to the it with the bundle. Then another fragment changes this URI via changeUri(Uri uri) method callback on the first fragment. If then the fragment gets recreated (for example due to screen rotation) it will use the first URI from the arguments bundle instead of the later updated uri, correct?
What is the best practice to solve this? Do I have to manually store it in the savedInstanceState and on usage decide whether to use the instanceState or arguments-bundle?
I'm looking for a standard way of handling the arguments of my fragments, so I think I'm going with such an approach (pseudo-code):
private Uri arg1;
public static Fragment newInstance(Uri arg1) {
create bundle
create fragment instance
set bundle to 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 I have a simply unified way to access my argument. And don't have to use the if-else-hassle, every time I need that argument.
What do you think about it?