Start an Activity from another Activity on Xamarin Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26543058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:45:57  来源:igfitidea点击:

Start an Activity from another Activity on Xamarin Android

androidandroid-activityxamarincall

提问by Lacho

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

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?

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

Thanks in advance.

Thanks in advance.

回答by Stam

You can write:

You can write:

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

and call it like:

and call it like:

 GoToActivity(typeof(ActivityType));

or just write:

or just write:

StartActivity(typeof(ActivityType));

回答by Luis Violas

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

in my code i did this

in my code i did this

回答by InitLipton

This is how i've done it in my Applicaiton

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 : YourBaseActivityis a base activity that you have created

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

回答by Snickbrack

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:

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

Maybe that helpful for some of you