Start an Activity from another Activity on Xamarin

2020-08-21 03:19发布

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.

4条回答
贼婆χ
2楼-- · 2020-08-21 03:55

You can write:

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

and call it like:

 GoToActivity(typeof(ActivityType));

or just write:

StartActivity(typeof(ActivityType));
查看更多
beautiful°
3楼-- · 2020-08-21 03:57

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

查看更多
女痞
4楼-- · 2020-08-21 03:57

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

查看更多
疯言疯语
5楼-- · 2020-08-21 04:17
void btnEntrar_Click(object sender,EventArgs e)
    { 
        var NxtAct= new Intent(this, typeof(Perguntas2));
        StartActivity(NxtAct);
    }

in my code i did this

查看更多
登录 后发表回答