Nougat - app data persistence

knottydevknottydev USMember ✭✭
edited December 2017 in Xamarin.Android

Why does Nougat not persist or rather hold app data files in some sort of limbo until the app is completely closed for the first time? I am running into an Xamarin bug or is this a feature of Nougat?

The issue is pretty simple to replicate now that I know what is happening. FileAccessHelper.GetLocalFilePath() below will create a text file here: Environment.GetFolderPath(Environment.SpecialFolder.Personal) and put a line of text in it.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        protected override void OnCreate(Bundle bundle)
            TabLayoutResource = Resource.Layout.Tabbar;
                    ToolbarResource = Resource.Layout.Toolbar;

        //loads/creates a text file, puts a single line it and returns the path which is: /data/user/0/com.myappid/files/test.txt
            string path = FileAccessHelper.GetLocalFilePath();



                   global::Xamarin.Forms.Forms.Init(this, bundle);

            LoadApplication (new App(path));

That line of text exists and I can check for it. But, it disappears every time the app is suspended and reloaded from the taskbar (using back button). However, if I completely close the app and reload, the file and line of text is persisted. A simple example project replicates this behavior on Nougat only. Even more strange, when the Forms PCL is initialized (LoadApplication (new App(path));) the file is found / exists even after suspending the app (and not closing it). However, in Android in the MainActivity, the file never exists between suspensions unless the app is completely closed!

Some hidden voodoo is going on here... what is it? I'm tempted to do a lot more investigating and I can share my entire sample project for this. But, just thought I'd ask first / not waste anymore than the 3 days it already took me to find / troubleshoot the issue people were reporting with Nougat only.

Sign In or Register to comment.