Good morning, I have a tabbed activity with three fragment, one of them is called frPrincipal and another one is called frConfiguracion. In frConfiguracion I have an editText where I enter an ip and a button with which I save using SharedPreferences. But I am not able that when the data is saved it is reflected in another editText of the frPrincipal. I have to close the application and reopen it to be able to see the data reflected.
This is what I have in the frConfiguracion button
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//debemos declarar esto para poder instanciar los objetos en el fragment
View rootView = inflater.inflate(R.layout.fragment_fr_configuracion,container,false);
if(rootView != null){
final EditText etConfigDirIP = (EditText) rootView.findViewById(R.id.etConfigDirIP);
Button btnConfigGuardar = (Button) rootView.findViewById(R.id.btnConfigGuardar);
//Traigo la IP anteriormente guardada
SharedPreferences sharpref = getActivity().getPreferences(Context.MODE_PRIVATE);
String valor = sharpref.getString("Dir_IP", "NO hay ip");
etConfigDirIP.setText(valor.toString());
btnConfigGuardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Utilizo el shared preferences para poder guardar los datos
SharedPreferences sharpref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharpref.edit();
editor.putString("Dir_IP",etConfigDirIP.getText().toString());
editor.commit();
Toast.makeText(getActivity(), "IP Guardada", Toast.LENGTH_LONG).show();
//Pendiente que al guardar actualice la IP en el fragment principal
}
});
}
return rootView;
}
And this is what I have in the frPrincipal
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fr_principal,container,false);
if(rootView != null){
//Instanciar
final EditText etPpalDirIP = (EditText) rootView.findViewById(R.id.etPpalDirIP);
final EditText etPpalDirCasa = (EditText) rootView.findViewById(R.id.etPpalDirCasa);
final EditText etPpalComando = (EditText) rootView.findViewById(R.id.etPpalComando);
final EditText etPpalMandar = (EditText) rootView.findViewById(R.id.etPpalMandar);
final EditText etPpalRespuesta = (EditText) rootView.findViewById(R.id.etPpalResultado);
Button btnMandar = (Button) rootView.findViewById(R.id.btnPpalMandar);
//Traigo la IP guardada en configuracion, mirar si se puede traer desde el edit text de configuracion
SharedPreferences sharpref = getActivity().getPreferences(Context.MODE_PRIVATE);
String valor = sharpref.getString("Dir_IP", "NO hay ip");
etPpalDirIP.setText(valor.toString());
//Metodo oncliclistener del boton
btnMandar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mensaje;
mensaje = etPpalDirIP.getText().toString() + "?"; //el interrogante es para separar el comando completo
mensaje = mensaje + etPpalDirCasa.getText().toString() + etPpalComando.getText().toString();
etPpalMandar.setText(mensaje);
//llamo el subprograma que manda al servidor
new MandarDatos().execute(mensaje);
//llevar el valor de la respuesta al edittext
}
});
}
return rootView;
}