4
I have a Fragment that contains an Edittext in which the user will enter his name.
In another Fragment is Textview which will receive the name typed in the previous Fragment.
Fragment Edit (where the user will enter the name):
public class Editar extends Fragment implements View.OnClickListener{
private EditText editar; private Button okBotao;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_editar, container, false);
editar = (EditText) rootview.findViewById(R.id.editar);
okBotao = (Button) getActivity().findViewById(R.id.ok);
okBotao.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
InicioActivity menu = new InicioActivity();
Bundle args = new Bundle();
args.putString("editar", editar.getText().toString());
menu.setArguments(args);
}
});
return rootview;
}
}
Fragment Start (where the name will be displayed):
public class Inicio extends Fragment {
TextView text;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_main, container, false);
TextView text = (TextView) rootview.findViewById(R.id.campoNome);
Bundle bundle = getArguments();
String editar = bundle.getString("editar");
text.setText(editar);
return rootview;
}
}
You didn’t make the change to
String.valueOf(editar)
. That looking at the question will think that this is a duplicate of the other and will vote to close.– ramaral
I changed it, but I sent the wrong code. I’ll send the updated one. Sorry.
– Rosilene
I don’t see how you have Fragment start displayed. onClick creates an instance of it, does the
setArguments()
but then do nothing else.– ramaral
Can I do this with Return menu? But then I would have to take the rootview Return, it’s not?
– Rosilene
Or I can use: [Fragmenttransaction transaction = getFragmentManager(). beginTransaction(); transaction.replace(R.id.container, menu); transaction.addToBackStack(null); transaction.commit();] ?
– Rosilene
It can but only in Activity. I think the solution would be the method
onClick
call a method of Activity and so on. Who should control the Fragments must be the Activity.– ramaral
Ah, then in case use within onCreat ((Minhaactivity)getActivity()). nameMethod(); ?
– Rosilene
In the
onClick(View v)
.((MinhaActivity)getActivity()).nomeDoMétodo(editar.getText().toString());
. Check whethergetActivity()
does not return null, should not but if return tell me.– ramaral
I can’t right now, but later I’ll put an answer to how to do that "correct".
– ramaral
Thank you so much for everything. I will try the last change you sent.
– Rosilene
I have the answer ready, but at the time I was reviewing it I noticed that you are getting a reference to okBotao using getActivity. Is the button in Fragment or is it in Activity? If it is in Activity everything we have said is no longer meaningful.
– ramaral
The button is in the Fragment.
– Rosilene