Passing custom objects between activities?

前端 未结 5 1244
忘掉有多难
忘掉有多难 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:19

    You should implement Parcelable interface.

    Link to documentation.

    0 讨论(0)
  • 2020-12-03 18:27

    a Parcel MIGHT solve your problem.

    think of a Parcel as an "array" (metaphorical) of primitive types (long, String, Double, int, etc). if your custom class is composed of primitive types ONLY, then change your class declaration including implements Parcelable.

    you can pass a parcelable object thru an intent with no difficulty whatsoever (just like you would send a primitive-typed object). in this case i have a parcelable custom class called FarmData (composed of longs, strings and doubles) which i pass from one activity to another via intent.

        FarmData farmData = new FarmData();
    // code that populates farmData - etc etc etc
        Intent intent00 = new Intent(getApplicationContext(), com.example.yourpackage.yourclass.class);
        intent00.putExtra("farmData",farmData);
        startActivity(intent00);    
    

    but retrieving it may be tricky. the activity that receives the intent will check if a bundle of extras was send along with the intent.

        Bundle extras = getIntent().getExtras();
        FarmData farmData = new FarmData();
        Intent intentIncoming = getIntent();
        if(extras != null) {
            farmData = (FarmData) intentIncoming.getParcelableExtra("farmData");// OK
        }
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-12-03 18:44

    One simple way to pass an object between activities or make a object common for all applicattion, is create a class extending Application.

    Here is a example:

    public class DadosComuns extends Application{
    
        private String nomeUsuario="";
    
        public String getNomeUsuario() {
            return nomeUsuario;
        }
    
        public void setNomeUsuario(String str) {
            nomeUsuario = str;
        }
    }
    

    In all your others activities, you just need instantiate one object "DadosComuns", declarating as a Global Variable.

    private DadosComuns dadosComuns;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        //dados comuns
        dadosComuns = ((DadosComuns)getApplicationContext());
    
        dadosComuns.setNomeUsuario("userNameTest"); }
    

    All others activities that you instantiate dadosComuns = ((DadosComuns)getApplicationContext()); you can acess getNomeUsuario() == "userNameTest"

    In your AndroidManifest.xml you need to have

    <application
            android:name=".DadosComuns"
    
    0 讨论(0)
  • 2020-12-03 18:46

    Given an object PasswordState that implements Serializable throughout the object tree, you can pass this object to anther activity as in:

    private void launchManagePassword() {
        Intent i= new Intent(this, ManagePassword.class); // no param constructor
        PasswordState outState= new PasswordState(lengthKey,timeExpire,isValidKey,timeoutType,"",model.getIsHashPassword());
        Bundle b= new Bundle();
        b.putSerializable("jalcomputing.confusetext.PasswordState", outState);
        i.putExtras(b);
        startActivityForResult(i,REQUEST_MANAGE_PASSWORD); // used for callback
    }
    
    0 讨论(0)
提交回复
热议问题