使用意图方法的通话活动

问题描述 投票:0回答:1

我知道Android OS需要参数构造器来重新创建Activity,如果需要,我可以使用bundle传递一些参数,如下所示:

private void OpenOtherActivityWindow_Click(object sender, EventArgs e)

{
      Intent nextActivity = new Intent(this, typeof(ThirdActivity));
      Dog mydog = new Dog("mydogName");
      Bundle bundle = new Bundle();
      bundle.PutSerializable("mydoggy", mydog);
      nextActivity.PutExtra("RowID", Convert.ToString(10));
      nextActivity.PutExtras(bundle);
      StartActivity(nextActivity);
}

[Activity(Label = "ThirdActivity")]
 public class ThirdActivity : Activity
 {
       protected override void OnCreate(Bundle savedInstanceState)
       {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.third);

            //Receive values if any from previous activity
            if (!Intent.HasExtra("mydoggy")) return;
            Dog tryme = (Dog)Intent.GetSerializableExtra("mydoggy");
            if (!Intent.HasExtra("RowID")) return;
            string text = Intent.GetStringExtra("RowID") ?? "0";
        }
}

尽管如此,仍然可以创建静态方法,该方法将从给定的参数(例如:)中返回我的意图:

static Intent CreateIntent(Dog dog, int rowID)

如果有人可以告诉我,那么它看起来与我在代码中显示的内容相反。

c# xamarin xamarin.android
1个回答
0
投票

我不知道您ThirdActivity的详细信息,但是我可以通过创建一个简单的演示来实现类似的功能。您可以在此处检查代码。

   [Activity(Label = "MovieDetailActivity")]
public class MovieDetailActivity : Activity
{
    public  TextView textView;

    public  static MovieModel mMoviemodel;// define your model here

    public  static int mRowID;  // define a int variable mRowID


    public  static Intent createIntent(Context context, MovieModel movie, int rowID)
    {
        Intent intent = new Intent(context, typeof(MovieDetailActivity));
        //Pass parameters here

        mMoviemodel = movie; 
        mRowID = rowID;

        return intent;
    }


  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.detaillayout);

        textView = FindViewById<TextView>(Resource.Id.info_textview);

        textView.Text = "movie name:" + mMoviemodel.mMovieName + " text = " + mRowID;

    }
}

用法:

   // pass your Object model
    StartActivity( MovieDetailActivity.createIntent(this, movie,10));
© www.soinside.com 2019 - 2024. All rights reserved.