Bundle only dates to another Fragment if the same has not passed through the attach.
Fragment does not communicate directly with another Fragment , Fragment has to communicate with Activity and Activity communicates with Fragment B (if the 2 Fragments are Up and running), for that you have to use an interface .
example:
1) Interface
public interface OnCommunicateInterface
{
void onSetText(String str);
}
2) Mainactivity
public class MainActivity extends FragmentActivity implements OnCommunicateInterface
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onSetText(String str)
{
FragmentReceiveData f2 = (FragmentReceiveData) getSupportFragmentManager().findFragmentById(R.id.frag_rec_data);
f2.updateText(str);
}
}
3) Fragment that will send Date
public class FragmentSendData extends Fragment
{
EditText editText;
Button button;
OnCommunicateInterface onCommunicate;
@Override
/****************** onCreateView *****************/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b)
{
View v = inflater.inflate(R.layout.fragment_send_data,container,false);
editText = (EditText)v.findViewById(R.id.editText);
button = (Button)v.findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String str = editText.getText().toString();
onCommunicate.onSetText(str);
}
};
button.setOnClickListener(listener);
// Inflate the layout for this fragment
return (v);
}
@Override
/**************** onAttach **************/
public void onAttach(Context context)
{
super.onAttach(context);
try
{
onCommunicate = (OnCommunicateInterface) context;
}
catch (Exception e)
{
Log.e("onAttach",e.toString());
}
}
}
4) Fragment to receive Date
public class FragmentReceiveData extends Fragment
{
TextView textView;
@Override
/************** onCreateView() ******************/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_receive_data,container,false);
textView = (TextView)v.findViewById(R.id.text_view);
// Inflate the layout for this fragment
return (v);
}
/*************** updateText() ********************/
public void updateText(String string)
{
textView.setText(string);
}
}
The cidogo is rather ugly , but it is easy to understand and it is a simple example.
hope it helps .
What do you mean, "I tried to steer the Object"? What error persists?
– Pablo Almeida
What kind of data do you want to pass on? At what point do you want to pass it on?
– ramaral
I try to access a method that is in another Ragment(these 2 Ragments are visible on the screen) and it returns me an error saying that I can’t access, I tried with thread and even then it didn’t work
– Roger Casagrande
See if this help, otherwise you have to give more details about what you want to do.
– ramaral