2

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;
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DeAbreu33
  • 45
  • 4
  • you want to clear the previous data that was input right ? – Chinmoy Debnath Mar 19 '14 at 03:17
  • 1
    so you means that your data should be persistent when your app is kill by user ??? if yes then you have to store your editttext value in sharedPrefrences . – mcd Mar 19 '14 at 03:20
  • No, for example I have my text field and I want to add my name and my age, I put my name and my age and I leave the application, then get back into the application and my name and age must be saved in the application . – DeAbreu33 Mar 19 '14 at 03:22
  • And how i do it this? Is my problem... Cause i'm new programming! – DeAbreu33 Mar 19 '14 at 03:23
  • @mcd can i do it that? – DeAbreu33 Mar 19 '14 at 03:27
  • use sharedPrefrences just like mcd told you. Fisrt look some tutorial of shared preference then do your work. http://developer.android.com/guide/topics/data/data-storage.html#pref – Chinmoy Debnath Mar 19 '14 at 03:29

2 Answers2

2

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

mcd
  • 1,434
  • 15
  • 31
1

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

Mark Pazon
  • 6,167
  • 2
  • 34
  • 50