简体   繁体   中英

Launching MainActivity after SplashActivity closes Android app

I am working in xamarin trying to make an Android project.

I am trying to learn Android and I am a little bit stuck. I have a MainActivity which uses to be the activity that launched on app start. Now i am adding a splash screen which means another activity, but I obviously want this to be the launched activity, and have it launch main activity after a period of time. It launched the splash activity first. Good. When it tries to launch the main activity from splash activity, it closes. I am using an example from the xamarin manual to make this splash activity with a few minor adjustments like making it inherit Activity rather than AppCompActivity.

https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

Note, MainActivity has worked quite well for some time, it is only crashing or closing now that it is not the main launcher app activity.

I know I am simply missing one important entry, and it probably has to do with the manifest file or a lack of a declaration in my cs file, as I've found this is why an app usually just quietly closes. A lot of the manifest declarations are made at run time in the cs files because they are from samples that I have seen and used. Here's my code. Can anybody tell me what I am missing? Sorry about the length, but I'm not sure what to include or not include.

This is the splash activity.

    [Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : Activity
    {
        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(2000); // Simulate a bit of startup work.
            Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
        }
    }

Here is my MainActivity. Note, it has more to it than posted here, as reflected by the permissions you will see in the manifest file, but it was too long to post here, and again, this activity always worked until it now has to be launched by another activity. I can add more upon request, but I think the problem is somewhere else

using System;
using System.IO;
using System.Net;
using System.Text;
using Android.Webkit;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Java.Interop;
using Android.Util;
using MyNameSpace.Services;
using Android.Preferences;
using Android.Graphics.Drawables;

namespace MyNameSpace
{
    [Activity(Label = "My Agent", Theme = "@android:style/Theme.NoTitleBar", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden)]

    public class MainActivity : Activity
    {
        WebView webview = null;
        urlPrefix = "http://example.com/";

        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);
            webview = new WebView(this);
            webview.Settings.JavaScriptEnabled = true;
            webview.Settings.SetGeolocationEnabled(true);
            webview.SetWebViewClient(new MyWebViewClient(this));
            webview.SetWebChromeClient(new MyWebChromeClient(this));
            SetContentView(webview);
            webview.LoadUrl(urlPrefix + "connection.aspx");

            //Set Notification bar but make the activity intent pending on user click of notification msg

            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.SingleTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);

            Notification.Builder builder = new Notification.Builder(this)
                    .SetContentTitle("My Agent Running")
                    .SetContentText("Show in forground")
                    .SetSmallIcon(Resource.Drawable.hippo72)
                    .SetContentIntent(pendingIntent);

            // Build the notification:

            notification = builder.Build();

            // Get the notification manager:

            notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

            // Publish the notification:

            notificationManager.Notify(notificationId, notification);

    public class MyWebViewClient : WebViewClient
    {
        private readonly Context _context;
        public MyWebViewClient(Context context)
        {
            _context = context;
        }
        public bool shouldOverrideUrlLoading(WebView view, String url)
        {
            var uri = Android.Net.Uri.Parse(url);
            var intent = new Intent(Intent.ActionView, uri);
            _context.StartActivity(intent);
            return true;
        }
    }

    public class MyWebChromeClient : WebChromeClient
    {
        private readonly Context _context;

        public MyWebChromeClient(Context context)
        {
            _context = context;
        }

        public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback)
        {
        callback.Invoke(origin, true, false);
    }
}

}

My manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mydomain.myagent" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
<application android:label="My Agent" android:icon="@drawable/hippo72"></application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
<uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />

Try this code in the SplashActivity.

with Finish(); and without tasks and async/await

[Activity(MainLauncher = true, Theme = "@style/MyTheme.Splash", NoHistory = true)]
public class SplashActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Intent startup = new Intent(this, typeof(MainActivity));
        StartActivity(startup);
        Finish();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM