Skip to content Skip to sidebar Skip to footer

Isharedpreferences Stored String Is Updating After Restart Of My App

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 searc

Solution 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();

publicclassFragment1 : 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);ISharedPreferencesprefs= PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
        mString = prefs.GetString("Data", " ");
        Viewv= inflater.Inflate(Resource.Layout.Fragment1,container,false);
        tv = v.FindViewById<TextView>(Resource.Id.textView1);
        tv.Text =mString;
        return v;
    }
}

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


        manager = FragmentManager;
        Fragment1fragment=newFragment1();
        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
          {
              vartext=edit.Text.ToString();
              ISharedPreferencesprefs= PreferenceManager.GetDefaultSharedPreferences(this);
              ISharedPreferencesEditoreditor= prefs.Edit();              
              editor.PutString("Data", text);
              editor.Apply();
              //editor.Commit();Fragmentfrg=null;
              frg = manager.FindFragmentByTag("myfragmanetag");
              manager.BeginTransaction().Detach(frg).Attach(frg).Commit();



          };
    }
}

Main.xaml

<LinearLayoutxmlns: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" ><EditTextandroid:id="@+id/editText1"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="pass data" /><FrameLayoutandroid: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>

Post a Comment for "Isharedpreferences Stored String Is Updating After Restart Of My App"