ISharedPreferences 存储的字符串在我的 APP 重启后更新

ISharedPreferences stored string is updating after restart of my APP

我正在将一个字符串从 Activity 传递给 Fragment 一切正常,但我传递的字符串不会立即更新它会在 app.Through google 搜索重新启动后更新 我尝试更改我的代码 "editor.commit" 到 "editor.apply" 但没用请帮我提建议,谢谢

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;
        }
    }

如果要刷新片段内容,可以分离片段并重新附加它。

这里是 Fragment1.cs,你只需要使用 OnCreateView 事件,它会在 运行 代码 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>