locked
How to pass parameter from one activity to another on Android? RRS feed

  • Question

  • User357114 posted

    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?

    Thursday, November 30, 2017 3:43 AM

Answers

  • User155041 posted

    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");
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 30, 2017 4:49 AM

All replies

  • User155041 posted

    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");
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 30, 2017 4:49 AM
  • User273322 posted

    @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 -_-

    Monday, January 8, 2018 2:36 PM
  • User155041 posted

    @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 :)

    Monday, January 8, 2018 3:56 PM
  • User380835 posted

    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);
    
    Saturday, May 4, 2019 6:45 AM