![](/img/trans.png)
[英]Start an Activity from DialogFragment in Android Xamarin Studio
[英]Start an Activity from another Activity on Xamarin Android
我發現這個 java 代碼創建了一個通用方法來從其他活動啟動任何活動。
public void gotoActivity(Class activityClassReference)
{
Intent i = new Intent(this,activityClassReference);
startActivity(i);
}
如何將該代碼轉換為 xamarin-Android 的 c#?
提前致謝。
你可以寫:
public void GoToActivity(Type myActivity)
{
StartActivity(myActivity);
}
並稱之為:
GoToActivity(typeof(ActivityType));
或者只寫:
StartActivity(typeof(ActivityType));
void btnEntrar_Click(object sender,EventArgs e)
{
var NxtAct= new Intent(this, typeof(Perguntas2));
StartActivity(NxtAct);
}
在我的代碼中我做到了這一點
這就是我在Applicaiton中完成的方式
public void StartAuthenticatedActivity(System.Type activityType)
{
var intent = new Intent(this, activityType);
StartActivity(intent);
}
public void StartAuthenticatedActivity<TActivity>() where TActivity: Activity
{
StartAuthenticatedActivity(typeof(TActivity));
}
然后,您可以添加TActivity : YourBaseActivity
是您創建的基本活動
適用於 Android 11+ 的閱讀(Android 11 引入了與包可見性相關的更改- 使用<queries>
元素)
您必須在調用應用程序的清單文件中聲明要訪問活動的應用程序的包名稱,否則您將收到ActivityNotFoundException
:
<manifest>
....
<queries>
<package android:name="com.companyname.yourOtherApp" />
</queries>
</manifest>
您要做的是首先獲取PackageManager:
PackageManager pm = Android.App.Application.Context.PackageManager;
然后,您可以通過以下方式尋找啟動 Activity 的意圖:
Intent intent = pm.GetLaunchIntentForPackage(packageName);
- 如果為空,則您可以通過邏輯將用戶帶到 PlayStore 以安裝該應用程序。
最終代碼:
PackageManager pm = Android.App.Application.Context.PackageManager;
Intent intent = pm.GetLaunchIntentForPackage(packageName);
if (intent != null)
{
intent.PutExtra(Android.Content.Intent.ExtraPackageName, "com.companyname.callingActivityName");
intent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
else
{
Android.App.Application.Context.StartActivity(new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse("https://play.google.com/store/apps/details?id=" + packageName)));
}
注意:請隨意使用 try/catch 解決所有這些問題。 行intent.PutExtra
只是如何為其他活動添加一些消息的示例。 您可以通過在OnCreate
使用來在另一邊獲取它:
var text = Intent.GetStringExtra(Android.Content.Intent.ExtraPackageName);
如果您想在OnResume
使用它,以便您的應用程序在它已經運行后重定向到時可以接收消息(在這種情況下 OnCreate 不會被命中),您必須首先覆蓋另一個方法。 它將允許您獲取更新的值:
protected override void OnNewIntent(Android.Content.Intent intent)
{
base.OnNewIntent(intent);
// Check not required, implement your own logic. This checks if a message was passed.
if (intent.Extras != null)
Intent = intent; // Intent.GetStringExtra now has the new value
}
現在在 OnResume 中,您可以使用與以前相同的方法,它將包含傳遞的值:
var text = Intent.GetStringExtra(Android.Content.Intent.ExtraPackageName);
我知道這個問題可能已經過時,但我有一個不同的方法,一個外部類用於對任何現有活動進行一般調用操作:
public static class GeneralFunctions
{
public static void changeView(Activity _callerActivity, Type activityType)
{
ContextWrapper cW = new ContextWrapper(_callerActivity);
cW.StartActivity(intent);
}
}
Button redirectButton = FindViewById<Button>(Resource.Id.RedirectButton);
redirectButton.Click += delegate
{
GeneralFunctions.changeView(this, typeof(LoginView));
};
也許這對你們中的一些人有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.