How to pass parameter from one activity to another on Android?

I am trying to pass parameter of item model from one Activity and display it in another Activity.

The code for the item class:

public class Item{
public Item();
public int Id {get; set;}
public string code {get;set;}
public string name{get;set;}
}

public class ActivityOne : Activity{

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.main);

Item item = new Item();
    item.Id = 1;
    item.code = "1001";
    item.name = "Sample Item";

    //Method 1

     var activity = new Intent(this, typeof(ActivityTwo));

     activity.PutExtra("Code", item.code);
     activity.PutExtra("Name", item.name);
     StartActivity(activity);

    //OR

    //Method 2

var activity = new Intent(this, typeof(ActivityTwo));

     activity.PutExtra("Id", item.Id);

     StartActivity(activity);

}

}

public class ActivityTwo : Activity{

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.main_layout);

    //Method 1
    string code = Intent.GetStringExtra("Code") ?? string.Empty;
    string name = Intent.GetStringExtra("Name") ?? string.Empty;

    //OR

    //Method 2

    string Id = Intent.GetStringExtra("Id") ?? string.Empty;
    Item item = new Item();
    item = itemRepo.Find(Convert.ToInt32(id));
}

}

Which way is best practice for Xamarin Android development?
Which method is good performance for app?

Best Answer

Answers

  • ynaeemynaeem ✭✭ SAMember ✭✭

    @Charwaka said:
    Hi > @Mr.Jordan

    you can pass whole object and desterilize that in another activity like this

    //To pass:
    intent.putExtra("Myitem", item);
    
    // To retrieve object in second Activity
    getIntent().getSerializableExtra("Myitem");
    

    thank you you helped me today -_-

  • CharwakaCharwaka ✭✭✭✭✭ INMember ✭✭✭✭✭

    @Mr.Jordan said:
    I am trying to pass parameter of item model from one Activity and display it in another Activity.

    The code for the item class:

    public class Item{
    public Item();
    public int Id {get; set;}
    public string code {get;set;}
    public string name{get;set;}
    }

    public class ActivityOne : Activity{

    protected override void OnCreate(Bundle savedInstanceState)
    {
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.main);

    Item item = new Item();
    item.Id = 1;
    item.code = "1001";
    item.name = "Sample Item";

    //Method 1

    var activity = new Intent(this, typeof(ActivityTwo));

         activity.PutExtra("Code", item.code);
         activity.PutExtra("Name", item.name);
         StartActivity(activity);
      
      //OR
      
      //Method 2
       
    

    var activity = new Intent(this, typeof(ActivityTwo));

         activity.PutExtra("Id", item.Id);
    
         StartActivity(activity);
      
    

    }
    }

    public class ActivityTwo : Activity{

    protected override void OnCreate(Bundle savedInstanceState)
    {
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.main_layout);

      //Method 1
      string code = Intent.GetStringExtra("Code") ?? string.Empty;
      string name = Intent.GetStringExtra("Name") ?? string.Empty;
      
      //OR
      
      //Method 2
      
      string Id = Intent.GetStringExtra("Id") ?? string.Empty;
      Item item = new Item();
      item = itemRepo.Find(Convert.ToInt32(id));
    

    }

    }

    Which way is best practice for Xamarin Android development?
    Which method is good performance for app?

    @ynaeem said:

    @Charwaka said:
    Hi > @Mr.Jordan

    you can pass whole object and desterilize that in another activity like this

    //To pass:
    intent.putExtra("Myitem", item);
    
    // To retrieve object in second Activity
    getIntent().getSerializableExtra("Myitem");
    

    thank you you helped me today -_-

    welcome :)

  • mac121mac121 ✭✭✭ Member ✭✭✭

    This worked for me, just as Charwaka, some modification in retrieving

        //To pass:
        Intent intent = new Intent(Application.Context, typeof(MainActivity));
        intent.PutExtra("Length", somedoublevalue);
    
        // To retrieve object in second Activity
        Intent.GetDoubleExtra("Length", defaultdoublevalue);
    
Sign In or Register to comment.