Hi in my Activity I have a fragmentA with a textview, when I click on the textview , this fragmentA is replaced with fragment which has a listview. Now when I click on the litsItem I have to goback to fragmentA and update the textview with the list item.
Implementation:
I created an interface in fragment,
public interface OnListItemSelectedListener {
public void onListItemSelected(String msg);
}
and in onAttach() I have the below code
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try {
mListener = (OnListItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnListItemSelectedListener");
}
}
In my listitem OnClickListener() I have this
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String message = parent.getItemAtPosition(position).toString();
mListener.onListItemSelected(message);
}
});
then in my activity i implemented the interface
@Override
public void onListItemSelected(String msg) {
// TODO Auto-generated method stub
ISOFragment myFrag = (ISOFragment)
getSupportFragmentManager().findFragmentById(R.id.isomain);
if (myFrag != null) {
myFrag.incrementdata(msg);
} else {
ISOFragment newFrag = new ISOFragment();
Bundle args = new Bundle();
args.putString("selecteitem", msg);
newFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, newFrag);
transaction.addToBackStack(null);
transaction.commit();
}
}
Now since the fragmentA is not available it goes to else part now how should i get the bundle arguments to fragmentA and update the textview.
Suppose I am going from A to B and coming back from B to A, if i get arguments and update the textview in Fragment A in onCreateView(), if when i run for the first time it is checking for the arguments which will be null.