Android intent to pass Objects between activities

Hi I’m trying to pass an object between activities but looks like the intent doesn’t find the activity.

There’s the obect class:

public class Contacte implements Serializable {

private String nom;
private String cognoms;
private String telefon;
private int horari; // 0 = horari sense definir; 1 = 24h; 2 = Mati; 3 = Tarda

/**
 * Constructor sense parametres que permet crear un objecte contacte amb els
 * valors predefinits
 */
public Contacte(){
    nom = "Nom";
    cognoms = "Cognom";
    telefon = "Telefon";
    horari = 0;
}

/** Gettes and Setters **/

The MainActivity:

// I use this cause I need to order the objects by priority
ArrayList<Contacte> contactes = new ArrayList<Contacte>();

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


    // Create Contactes and add to the ArrayList
    Contacte c1 = new Contacte();
    Contacte c2 = new Contacte();
    Contacte c3 = new Contacte();

    c1.setNom("John");
    c1.setCognoms("Doe");
    c1.setTelefon("632145897"); 

    contactes.add(c1);
    contactes.add(c2);
    contactes.add(c3);


 // This is because I have 6 different buttons
    Button edita1 = (Button)findViewById(R.id.edita1);
    edita1.setOnClickListener(onClickListener);


}


private OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {
        switch(v.getId()){
            case R.id.edita1:

                // 1. create an intent pass class name or intnet action name
                Intent i = new Intent("com.ioc.eac1.EditaContacte");

                // 2. create person object
                Contacte c = new Contacte();
                c = contactes.get(0);

                // 3. put person in intent data
                i.putExtra("contacte", c);

                // 4. start the activity
                startActivity(i);
            break;
            case R.id.edita2:
                 //DO something
            break;
            case R.id.edita3:
                 //DO something
            break;
        }

  }

};

The EditaContacte:

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

    // 1. get passed intent
    Intent intent = getIntent();

    // 2. get person object from intent
    Contacte c = (Contacte) intent.getSerializableExtra("contacte");

    // 3. get reference to person textView
    EditText editaNom = (EditText) findViewById(R.id.editaNom);

    // 4. display name & age on textView
    editaNom.setText(c.getNom());

}

And there’s the Manifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".EditaContacte"
        android:label="@string/title_activity_edita_contacte" >
    </activity>


</application>

I’m trying to debug it and looks like th intent doesn’t find the activity. But the activity it si and is declared on the manifest. I’m trying to send and object created in one activity when button is on click and show object properties on other activity

Answer

Just use Intent.SetClass(MainActivity.this, EditaContacte.class).

If the two activities are in the same app,there is no need to use action,just set class.

Leave a Reply

Your email address will not be published. Required fields are marked *