简体   繁体   中英

Pass object in Intent

I would like pass object with Intent:

public class First extends Activity implements Serializable {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button here = (Button) findViewById(R.id.here);

        here.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), MyClass.class);
                Test test = new Test();
                intent.putExtra("test", test);  
                startActivity(intent);
            }
        });
    }

}

but this in line with intent.putExtra("test", test); return me error:

The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, Test)

How can i make it?

Change the class header of Test to this

public Test implements Parcelable ...

use the next examples and create the Test class to implemet the Parcelable correcly. Parcelable Example Parcelable Android Developers SDK

and it should work.,

ok new Solution change it to

public Test implements Serializable ...

no need to implement anything

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