简体   繁体   中英

Saving data from Text Field

My application has some editable text fields but not saved what he added, I want to exit the application and when re-entering my data still there.

XML

 <DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="28dp"
    android:layout_marginTop="28dp"
    android:text="DigitalClock"
    android:textColor="#FA8072"
    android:textSize="40dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/digitalClock1"
    android:layout_toLeftOf="@+id/digitalClock1"
    android:text="MEDICINES"
    android:textColor="#228B22"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="20dp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="39dp"
    android:ems="10" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="Name" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="8dp"
    android:text="Name" />

<EditText
    android:id="@+id/EditText01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView01"
    android:layout_centerVertical="true"
    android:layout_marginTop="8dp"
    android:ems="10" />

</RelativeLayout>

JAVA

    public PagesFragment(){}

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

    View rootView = inflater.inflate(R.layout.fragment_pages, container, false);

    return rootView;
}
}

you can use bundle to store the edittext value on onSaveInstanceState. and check condition in oncreate method whether bundle object is null or not if it's not null then retrieve from it using get* method. and if you want to store your data persistent way throughout your app you can used sharedprefrensce .

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

    //For storing string value in sharedPreference     
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("Name","Harneet");
    editor.commit();

    //For retrieving string value from sharedPreference
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String name = preferences.getString("Name","");

You can try using SharedPreference . Override onPause() in your activity and store the text in a SharedPreference. In the activity's onResume(), retrieve it from the SharedPreference and load it back to your EditText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM