Passing custom objects between activities?

前端 未结 5 1272
忘掉有多难
忘掉有多难 2020-12-03 17:43

How do I pass custom objects between activites in android? I\'m aware of bundles but I can\'t seem to see any functionality for this in them. Could anyone show me a nice exa

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 18:40

    Using Parcelable interface you can pass custom java object into the intent.

    1) implement the Parcelable interface to your class like:

    class Employee implements Parcelable
    {
    }
    

    2) Pass the Parcelable object into the intent like:

    Employee mEmployee =new Employee();
    Intent mIntent = new Intent(mContect,Abc.class);
    mIntent.putExtra("employee", mEmployee);
    startActivity(mIntent);
    

    3) Get the data into the new [Abc] Activity like:

    Intent mIntent  = getIntent();
    Employee mEmployee  = (Employee )mIntent.getParcelableExtra("employee");
    

提交回复
热议问题