我正在将字符串从活动传递到片段,一切正常,但我传递的字符串没有立即更新,它会在重新启动应用程序后更新。通过谷歌搜索,我尝试将我的代码"editor.commit"更改为"editor.apply",但没有用,请帮助我提供您的建议,谢谢
活动.cs
var text = newSentence.ToString();
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("Data", text);
editor.Apply();
editor.Clear();
片段.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 事件,它会在管理器中运行代码时触发。BeginTransaction((。分离(frg(。附件(frg(。提交((;
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>
片段1.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>