简体   繁体   中英

Call activity using intent method

I know that Android OS need paramterles constructor to recreate Activity and i could use bundle to pass some arguments if required as follows:

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

Nevertheless is it possible to create static method which would return intent for me from given parameters like?:

static Intent CreateIntent(Dog dog, int rowID)

If so could someone show me then how it could look like as opposite to whati show in my code please.

I don't know the details of your ThirdActivity , but I could achieve the similar function by creating a simple demo. You can check the code here.

   [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;

    }
}

Usage:

   // pass your Object model
    StartActivity( MovieDetailActivity.createIntent(this, movie,10));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM