'How to create editText in fragment

I'd like to create editable textfield in fragment, which after close or stop of app would be saved. But there's something wrong in line with return notatki; I already have this:

public class DetailFragment2 extends Fragment {

    private EditText notatki;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");


    }     
    @SuppressLint("SimpleDateFormat")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.details2, parent, false);
        notatki = (EditText) view.findViewById(R.id.editText1);
        SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
        notatki.setText(settings.getString("value", ""));
        return view;
    }  
    @Override
    public void onStop( ){
        super.onStop();
        if(notatki.getText() != null) {
            SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("value", notatki.getText().toString());
            editor.commit();
        }
    }
}

When I change return notatki; to return view; it works until the stop of app, when I wanted to save content of editText but it isn't saving anything.



Solution 1:[1]

Change

EditText notatki = (EditText) view.findViewById(R.id.editText1);

to

notatki = (EditText) view.findViewById(R.id.editText1);

because if you declare it again in your onCreateView it will shadow the global one and you will get it null in onStop

Solution 2:[2]

Try This....

public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.details2, parent, false);
    EditText notatki = (EditText) view.findViewById(R.id.editText1);
    SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
    notatki.setText(settings.getString("value", ""));
    return view;
}  

Solution 3:[3]

You are doing it right but the problem is that you should return the parent view from onCreateView and not the EditText instance notatki

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.details2, parent, false);
        ....
        return view;
    }  

Solution 4:[4]

private EditText etext;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.secondfragment,container,false);
        etext = (EditText) getView().findViewById(R.id.editText4);
        return v;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Apoorv
Solution 2 Abhishek Patel
Solution 3 Lalit Poptani
Solution 4 Volverine