ISharedPreferences stored string is updating after restart of my APP

半腔热情 提交于 2019-12-13 20:23:45

问题


I am passing a string from an Activity to Fragment everything works perfectly but my passed string is not updating instantly it updates after restart of my app.Through google search I tried changing my code "editor.commit" to "editor.apply" but no use please help me with your suggestions, thanks

Activity.cs

var text = newSentence.ToString();
 ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
 ISharedPreferencesEditor editor = prefs.Edit();
 editor.PutString("Data", text);
 editor.Apply();
 editor.Clear();

Fragment.cs

public class Fragment1 : Android.Support.V4.App.Fragment
    {
        public string mString;
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
            mString = prefs.GetString("Data", " ");

        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            var view = inflater.Inflate(Resource.Layout.Fragment1, container, false);

            var textView = view.FindViewById<TextView>(Resource.Id.txtView);
            textView.Text = mString;

            return view;
        }
    }

回答1:


If you want to refresh fragment content, you can detach the fragment and reattach it.

Here is the Fragment1.cs, you just need to use OnCreateView event, it will fire when run code in manager.BeginTransaction().Detach(frg).Attach(frg).Commit();

public class Fragment1 : Fragment
{
    private TextView tv;
    public string mString;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
        mString = prefs.GetString("Data", " ");
        View v = inflater.Inflate(Resource.Layout.Fragment1,container,false);
        tv = v.FindViewById<TextView>(Resource.Id.textView1);
        tv.Text =mString;
        return v;
    }
}

public class MainActivity : AppCompatActivity
{
    private Button btn1;
    private EditText edit;          
    private static Android.App.FragmentManager manager;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);


        manager = FragmentManager;
        Fragment1 fragment = new Fragment1();
        manager.BeginTransaction().Add(Resource.Id.frameLayout1, fragment,"myfragmanetag").Commit();

        // t.Add(Resource.Id.fragment1,fragment);

        btn1 = FindViewById<Button>(Resource.Id.button1);
        edit= FindViewById<EditText>(Resource.Id.editText1);
        btn1.Click += delegate
          {
              var text =edit.Text.ToString();
              ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
              ISharedPreferencesEditor editor = prefs.Edit();              
              editor.PutString("Data", text);
              editor.Apply();
              //editor.Commit();
              Fragment frg = null;
              frg = manager.FindFragmentByTag("myfragmanetag");
              manager.BeginTransaction().Detach(frg).Attach(frg).Commit();



          };
    }
}

Main.xaml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText android:id="@+id/editText1" android:layout_width="match_parent" 
        android:layout_height="wrap_content" />
<Button android:id="@+id/button1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" android:text="pass data" />


<FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" 
        android:layout_height="match_parent" />

    </LinearLayout>

Fragment1.xaml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:text="this is test" />
   </LinearLayout>


来源:https://stackoverflow.com/questions/58086556/isharedpreferences-stored-string-is-updating-after-restart-of-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!