0
Helo我正在爲我的應用程序添加一個輔助功能,當用戶在主頁按鈕上按住時,用戶可以點擊的卡片打開一個android活動然後打開有問題的xamarin表單頁面,例如當單擊數學卡時MathPage xamarin表單頁面打開這個工作,但如果我有在後臺運行的xamarin表單應用程序,它加載主屏幕布局,無論我點擊哪個按鈕。如果我從多任務中關閉xamarin表單應用程序,並按住home鍵並點擊數學卡片,它將打開MathPage。如何重置Xamarin從Xamarin droid形成導航堆棧協助活動
繼承人我的android活動代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;
namespace Appname.Droid
{
[Activity(LaunchMode = LaunchMode.SingleInstance, Theme = "@style/Theme.Transparent")]
[IntentFilter(new[] { Intent.ActionAssist }, Categories = new[] { Intent.CategoryDefault })]
public class ToolBelt : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.ToolBelt);
}
//Code That Opens The Math Part!
[Java.Interop.Export("math")] // The value found in android:onClick attribute.
public void btnOneClick4(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Math");
StartActivity(intent);
}
//Code That Opens The Science Part!
[Java.Interop.Export("science")] // The value found in android:onClick attribute.
public void btnOneClick9(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Science");
StartActivity(intent);
}
//Code That Opens The Handwriting Part!
[Java.Interop.Export("english")] // The value found in android:onClick attribute.
public void btnOneClick10(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Handwriten");
StartActivity(intent);
}
//Code That Opens The Flascards Part!
[Java.Interop.Export("flashcard")] // The value found in android:onClick attribute.
public void btnOneClick11(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Flashcards");
StartActivity(intent);
}
//Code That Opens The Internet App!
[Java.Interop.Export("web")] // The value found in android:onClick attribute.
public void btnOneClick8(View v) // Does not need to match value in above attribute.
{
var uri = Android.Net.Uri.Parse("http://www.google.com");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
}
//Code That Opens The Gmail App!
[Java.Interop.Export("email")] // The value found in android:onClick attribute.
public void btnOneClick3(View v) // Does not need to match value in above attribute.
{
var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.gm");
StartActivity(intent);
}
//Code That Opens The Books App!
[Java.Interop.Export("books")] // The value found in android:onClick attribute.
public void btnOneClick5(View v) // Does not need to match value in above attribute.
{
var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.apps.books");
StartActivity(intent);
}
}
}
這裏是在MainActivity機器人代碼:
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using AppName.Droid;
using AppName.Data;
[assembly: Xamarin.Forms.Dependency(typeof(MainActivity))]
namespace AppName.Droid
{
[Activity(Label = "AppName", Icon = "@drawable/ic_launcher", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, SchoolTools.PackageInterface
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
var page = Intent.GetStringExtra("page");
LoadApplication(new App(page));
}
public IList<Apps> GetInstalledApps()
{
IList<Apps> apps = new List<Apps>();
var pkgInfos = global::Xamarin.Forms.Forms.Context.PackageManager.GetInstalledPackages(PackageInfoFlags.Activities);
foreach(var pi in pkgInfos)
{
// skip system packages
if (pi.ApplicationInfo.DataDir.StartsWith("/data/user/"))
{
Apps app = new Apps(pi.ApplicationInfo.LoadLabel(global::Xamarin.Forms.Forms.Context.PackageManager).ToString(), pi.PackageName);
if (!apps.Contains(app))
{
apps.Add(app);
}
}
}
return apps;
}
public bool Launch(string package)
{
var intent = Application.Context.PackageManager.GetLaunchIntentForPackage(package);
bool retVal = IsIntentAvailable(intent);
if (retVal)
{
intent.AddFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
}
return retVal;
}
private static bool IsIntentAvailable(Intent intent)
{
return intent != null && Application.Context.PackageManager.QueryIntentActivities(intent, 0).Count != 0;
}
}
}
繼承人我app.cs代碼:
using System;
using Xamarin.Forms;
namespace AppName
{
public class App : Application
{
public App(string pageName = "AppNameHome")
{
switch (pageName)
{
case "Math":
MainPage = new NavigationPage(new MathPage());
break;
case "Science":
MainPage = new NavigationPage(new Science.ScienceToolsPage());
break;
case "Handwriten":
MainPage = new NavigationPage(new Handwriting.HandwritingToolsPage());
break;
case "Flashcards":
MainPage = new NavigationPage(new Flashcards.FlashCardHome());
break;
default:
MainPage = new NavigationPage(new AppNameHome());
break;
}
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
任何幫助將是驚人的!
感謝提前:)