Lacho
Lacho

Reputation: 145

Start an Activity from another Activity on Xamarin Android

I found this java code to create a generic method to start any activity from other activity.

public void gotoActivity(Class activityClassReference)
{
    Intent i = new Intent(this,activityClassReference);
    startActivity(i);
}

How can I convert that code to c# for xamarin-Android?

Thanks in advance.

Upvotes: 12

Views: 35359

Answers (5)

iBobb
iBobb

Reputation: 1160

Read for Android 11+ (Android 11 introduces changes related to package visibility - use the <queries> element)

You have to declare the package names for apps you want to access activities from in the manifest file of the calling app, otherwise you will get ActivityNotFoundException:

<manifest>
....
    <queries>
        <package android:name="com.companyname.yourOtherApp" />
    </queries>
</manifest>

What you want to do is to first get the PackageManager:

PackageManager pm = Android.App.Application.Context.PackageManager; Then you can look for an intent to launch the Activity with:

Intent intent = pm.GetLaunchIntentForPackage(packageName); - If that's null, you could have logic to take the user to the PlayStore to install that app.

Final code:

            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)));
            }

Notes: Feel free to use try/catch around all of this. The line intent.PutExtra is just a sample how to add some message for the other activity. You can get it on the other side by using in OnCreate:

var text = Intent.GetStringExtra(Android.Content.Intent.ExtraPackageName);

If you want to use that in OnResume, so that your app can receive a message if redirected to after it's already running (in which case OnCreate will not be hit), you'd have to override another method first. It will allow you to get the updated value:

    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
    }

Now in OnResume, you can use the same method as before and it will contain the passed value:

var text = Intent.GetStringExtra(Android.Content.Intent.ExtraPackageName);

Upvotes: 3

Snickbrack
Snickbrack

Reputation: 956

I know the question may be outdated but I have a different approach, with an external class for a general call action towards any existing activity:

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));
};

Maybe that helpful for some of you

Upvotes: 0

Luis Violas
Luis Violas

Reputation: 75

void btnEntrar_Click(object sender,EventArgs e)
    { 
        var NxtAct= new Intent(this, typeof(Perguntas2));
        StartActivity(NxtAct);
    }

in my code i did this

Upvotes: 6

InitLipton
InitLipton

Reputation: 2453

This is how i've done it in my 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));
    }

You can then add in the where TActivity : YourBaseActivity is a base activity that you have created

Upvotes: 2

Stam
Stam

Reputation: 2490

You can write:

public void GoToActivity(Type myActivity)
{
            StartActivity(myActivity);
}

and call it like:

 GoToActivity(typeof(ActivityType));

or just write:

StartActivity(typeof(ActivityType));

Upvotes: 19

Related Questions