I have a fragment:
ContactUsFragment.java
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
/**
*
*/
public class ContactUsFragment extends Fragment {
private EditText et_name, et_phoneno;
private String name = "", phone = "";
private boolean instanceSaved = false;
public ContactUsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
Toast.makeText(getActivity().getBaseContext(), "Not null", Toast.LENGTH_LONG).show();
name = savedInstanceState.getString("sv_name");
phone = savedInstanceState.getString("sv_phone");
instanceSaved = savedInstanceState.getBoolean("InstanceSaved");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_contact_us, container, false);
registerETs(view);
if(instanceSaved) {
Toast.makeText(getActivity().getBaseContext(), "Instance was saved", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getBaseContext(), name, Toast.LENGTH_LONG).show(); //--This line works. Gives the correct value of string upon orientation change--
Toast.makeText(getActivity().getBaseContext(), phone, Toast.LENGTH_LONG).show();
et_name.setText(name); //--This line does not work--
et_name.setText("Another String"); //--This line does not work--
et_phoneno.setText(phone); //--This line does not work--
}
et_name.setText("A String"); //--This line works--
return view;
}
@Override
public void onStart() {
super.onStart();
}
public void registerETs(View view) {
et_name = (EditText)view.findViewById(R.id.input_name);
et_phoneno = (EditText)view.findViewById(R.id.input_phoneno);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Toast.makeText(getActivity().getBaseContext(), "saving", Toast.LENGTH_LONG).show();
savedInstanceState.putString("sv_name", et_name.getText().toString());
savedInstanceState.putString("sv_phone", et_phoneno.getText().toString());
savedInstanceState.putBoolean("InstanceSaved", true);
}
}
The problem I am facing is that, the et_name.setText(name)
inside if(instanceSaved){...}
is not working. It sets nothing. As if, name is getting null
value from if(savedInstanceSaved != null){...}
. But the Toasts
that I have used inside my various blocks of code is working as expected. It is saving in onSaveInstanceState(Bundle savedInstanceState){...}
, executing if(savedInstanceSaved != null){...}
, meaning my savedInstanceState
is not null and also showing the correct value for name
string in if(instanceSaved){...}
. That means my code is working correctly during an orientation
change. But it is not setting any text in setText(String)
method.
I also tried using setText("String")
, and that works. As shown in code. What wrong am I doing and what am I missing? Please help. Thank you.
NOTE: I am using
EditText
s insideandroid.support.design.widget.TextInputLayout
EDIT: I found that no matter what,
setText(name)
is always taking the initial value ofString name
. Even thoughname
inToast
shows the new updated value,setText()
is somehow always using the initial value. Also, I noticed that if I change the orientation thrice, the app crashes.