Skip to content

Instantly share code, notes, and snippets.

@glennstephens
Created February 21, 2019 11:00
Show Gist options
  • Save glennstephens/ea77a7a5d2d4cd16af19e33a654de840 to your computer and use it in GitHub Desktop.
Save glennstephens/ea77a7a5d2d4cd16af19e33a654de840 to your computer and use it in GitHub Desktop.
A simple modification/POC to test the Foldable support in Xamarin.Android
/*
Looking at the Samsung Fold, I wanted to see what it would take to support it from a Xamarin.Android app
It turns out, not a great deal to test the proof of concept.
Step 1. Download the emulator APK at https://developer.samsung.com/galaxy/foldable/test and follow the instructions to install it
Step 2. Update the ConfigurationChanges property in the Activity Attribute - see line 24
Step 3. Override OnSaveInstanceState and OnRestoreInstanceState to save/restore state
Step 4. Perform any updates (if needed) in OnConfigurationChanged
Step 5. Add the MetaData attribute for Allowing Multiple Resumed Activities
*/
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content.PM;
using Android.Content.Res;
[assembly: MetaData("android.allow_multiple_resumed_activities", Value ="true")]
namespace TestApp
{
[Activity(Label = "TestApp",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.SmallestScreenSize | ConfigChanges.ScreenLayout,
MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate { button.Text = $"{count++} clicks!"; };
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
outState.PutInt("counter", count);
}
protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
base.OnRestoreInstanceState(savedInstanceState);
count = savedInstanceState.GetInt("counter", 0);
}
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
// Update any visuals needed here
}
}
}
@nonubitta
Copy link

Awesome, thank you so much for the tip. My app used to crash when opening the fold. Now it opens the same page again.
But I do get one issue, it starts to flicker when unfolded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment