2

I have some of text blocks in activity, but when I change activity text in it dissapears.

How I can save text?

I know it is Shared preferences in Android. But how I can save text in activity by using it?

Code

private void Display (){
        LinearLayout display2 = FindViewById<LinearLayout> (Resource.Id.linearLayout12);        
        //LinearLayout display = FindViewById<LinearLayout> (Resource.Id.linearLayout13);           
        TextView productname = FindViewById<TextView> (Resource.Id.posttittle);
        TextView price = FindViewById<TextView> (Resource.Id.price);
        TextView weight = FindViewById<TextView> (Resource.Id.weight);


        productname.Text = Intent.GetStringExtra ("title");

        if (productname.Text == Intent.GetStringExtra ("title")) {
            display2.Visibility = ViewStates.Visible;
        } 
        else {
            display2.Visibility = ViewStates.Gone;
        }


        price.Text = Intent.GetStringExtra("price");
        weight.Text = Intent.GetStringExtra("weight");
        //display2.Visibility = ViewStates.Visible;
        productname.Visibility = ViewStates.Visible;
        price.Visibility = ViewStates.Visible;
        weight.Visibility = ViewStates.Visible;
    }

My Axml

<LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout12">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="147.6dp"
                android:id="@+id/linearLayout13"
                android:minWidth="25px"
                android:minHeight="25px">
                <LinearLayout
                    android:orientation="horizontal"
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="276.2dp"
                    android:layout_height="match_parent"
                    android:id="@+id/linearLayout24">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:layout_width="244.3dp"
                        android:layout_height="97.3dp"
                        android:id="@+id/posttittle"
                        android:layout_marginTop="22.4dp"
                        android:paddingTop="32dp"
                        android:layout_marginLeft="16.8dp" />
                </LinearLayout>
                <LinearLayout
                    android:orientation="vertical"
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="173.1dp"
                    android:layout_height="match_parent"
                    android:id="@+id/linearLayout25">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="59.9dp"
                        android:layout_height="43.8dp"
                        android:id="@+id/weight"
                        android:layout_marginTop="54.6dp"
                        android:text="330u"
                        android:layout_marginLeft="5.6dp" />
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="82.3dp"
                        android:layout_height="42.4dp"
                        android:id="@+id/price"
                        android:text="88.00uhy" />
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:id="@+id/linearLayout23"
                android:background="#99999999" />
        </LinearLayout>

2 Answers2

0

Follow this tutorial :). Will show you how to use shared preferences so you can save the text.

Shared Preferences

Then when you have the value you wanted saved. You can call upon shared preferences and the value you have saved and set it in the text.

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
  • I can write text to value and call this value from all activities? –  Nov 03 '15 at 12:20
  • Yes :). A shared preference is basically a file created with values stored by you like a key value type. You then just call the value in your on create and store is as a field :). They are very handy! You could start looking into using the sqlite database too :) – SmiffyKmc Nov 03 '15 at 12:40
  • Also a very handy tutorial in looking up! http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/ – SmiffyKmc Nov 03 '15 at 12:43
  • Also if you are looking a cross-device solution, Akavache is a good solution : https://github.com/akavache/Akavache – SushiHangover Nov 03 '15 at 12:52
0

If you want retain the text values between activities you can use onSaveInstanceState(Bundle savedInstanceState) and save the values in the bundle as name-value pair.

To get values from the bundle you can use onRestoreInstanceState().

Refer to this answer for more details.

SharedPreferences can be used to maintain the values even after the app is killed, onSaveInstanceState is suitable to maintain the states between activities.

Community
  • 1
  • 1
rakex
  • 1,999
  • 2
  • 16
  • 17