Reputation: 70
I have built the app which uses a webview in xamarin and I have tested them on many devices including the Galaxy S7 edge as I thought the reason for it not working is because of the Edge screen. However, it works fine on the S7 Edge but on the S8 Edge devices that I have tried them on, they instantly crash.
There's only two classes that I use:
MainActivity:
namespace APPNAME.Android
{
[Activity(Label = "APPNAME", Icon = "@drawable/icon", MainLauncher =
false,
ConfigurationChanges = ConfigChanges.ScreenSize |
ConfigChanges.Orientation)]
public class MainActivity : Activity
{
WebView web_view;
//private bool appeared;
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string
url)
{
view.LoadUrl(url);
return false;
}
}
protected override void OnCreate(Bundle bundle)
{
Window.RequestFeature(WindowFeatures.NoTitle);
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.SetWebViewClient(new HelloWebViewClient());
web_view.LoadUrl("http://link.com");
}
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back && web_view != null)
{
try
{
if (web_view.CanGoBack())
{
Log.Debug("StackOverflow", "Allow browser back");
// Toast.MakeText(this, "Going back",
ToastLength.Short).Show();
web_view.GoBack();
return true;
}
}
catch (Exception ex)
{
Log.Error("StackOverflow", ex.Message);
}
}
{
//Log.Error("StackOv20erflow", "Null webview...");
//StartActivity(typeof(MainActivity));
//Finish();
OnBackPressed();
}
//Log.Debug("StackOverflow", "Back button blocked");
//Toast.MakeText(this, "Back button blocked",
ToastLength.Short).Show();
return false;
}
public override void OnBackPressed()
{
Intent startMain = new Intent(Intent.ActionMain);
startMain.AddCategory(Intent.CategoryHome);
startMain.SetFlags(ActivityFlags.NewTask);
StartActivity(startMain);
}
}
}
Splash Screen:
namespace APPNAME.Android
{
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory =
true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState,
PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
// Simulates background work that happens behind the splash screen
async void SimulateStartup()
{
//Log.Debug(TAG, "Performing some startup work that takes a bit of
time.");
//await Task.Delay(1000); // Simulate a bit of startup work.
//Log.Debug(TAG, "Startup work is finished - starting
MainActivity.");
//StartActivity(new Intent(Application.Context,
typeof(MainActivity)));
await Task.Delay(4000);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionName="1.0.0.8" package="APPNAME.android"
android:installLocation="auto" android:versionCode="8">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25" />
<application android:label="APPNAME" android:icon="@drawable/Icon"
android:debuggable="false"></application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Not sure where to start fixing the issue as I don't know what's the cause of it.
Any help would be appreciated!
Upvotes: 1
Views: 913
Reputation: 247373
Avoid using async void
unless in an event handler. And also NEVER create a new Task
and use task.Start
Reference Async/Await - Best Practices in Asynchronous Programming
That said I would advise you create an event and handler in the SplashActivity
public class SplashActivity : AppCompatActivity {
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
base.OnCreate(savedInstanceState, persistentState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
// Launches the startup task
private event EventHandler Starting = delegate { }
protected override void OnResume() {
base.OnResume();
//subscribe to event
Starting += Activity_Starting;
//raise event
Starting (this, EventArgs.Empty);
}
// Simulates background work that happens behind the splash screen
async void Activity_Starting(object sender, EventArgs e) {
//unsubscribe
Starting -= Activity_Starting;
//and carry on
//Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
//await Task.Delay(1000); // Simulate a bit of startup work.
//Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
//StartActivity(new Intent(Application.Context, typeof(MainActivity)));
await Task.Delay(4000);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
Upvotes: 1