I have one activity with container for fragments. Fragment1 is for example list of products. On search action, i replace container with Fragment2 (search-filter panel) and need on back-button send some data to popped from backstack Fragment1. How to do that ? I have static create
method on all fragments (with setArguments
use), but i don't want to create new instance. Maybe i've something missed in using of fragments, but know only about setArguments
method, but this method is only usable to creating new instances of fragments.
You could store the Data in the MainActivity and read it from any Fragment like:
//MainActivity
private String mSearchItem;
public void getSearchItem(){
return this.mSearchItem;
}
public void setSearchItem(String searchItem){
this.mSearchItem = searchItem;
}
//FRAGMENT 1
public void onResume(){
super.onResume();
String item = ((MainActivity)getActivity()).getSearchItem();
//if item != null do some crazy shizzle
//set MainActivity searchitem to null to avoid reloading it
}
//FRAGMENT 2
private void setData(String searchItem){
((MainActivity)getActivity()).setSearchItem(searchItem);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments