Uno dei modi più semplici per passare informazioni tra attività è di utilizzare Pacchetti.
È possibile aggiungere extra a un intento prima di avviare una nuova attività.
D'altra estremità (l'altra attività) è possibile ricevere questi extra fuori del fascio ed eventualmente ricostruito l'oggetto nella nuova attività ..
Ecco un esempio:
Prima di tutto, ecco la classe Studente:
Student Class - Student.java:
package com.stephendiniz.objectsharing;
public class Student
{
int id;
String name;
String profession;
Student()
{
id = 0;
name = "";
profession = "";
}
Student(int id, String name, String profession)
{
this.id = id;
this.name = name;
this.profession = profession;
}
public int getId() { return id; }
public String getName() { return name; }
public String getProfession() { return profession; }
}
La principale attività farà riferimento la classe Student per generare un oggetto s
attività - AndroidObjectSharing.java:
package com.stephendiniz.objectsharing;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidObjectSharingActivity extends Activity
{
TextView id;
TextView name;
TextView profession;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
id = (TextView)findViewById(R.id.id);
name = (TextView)findViewById(R.id.name);
profession = (TextView)findViewById(R.id.profession);
Button submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener()
{
final Intent newActivity = new Intent(AndroidObjectSharingActivity.this, NewActivity.class);
public void onClick(View v)
{
Student s = new Student(Integer.parseInt(id.getText().toString()), name.getText().toString(), profession.getText().toString());
newActivity.putExtra("extraStudentId", s.getId());
newActivity.putExtra("extraStudentName", s.getName());
newActivity.putExtra("extraStudentProfession", s.getProfession());
startActivity(newActivity);
}
});
}
}
Ecco il XML che è legato alla classe Studente:
Layout XML principale - main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student ID:" />
<EditText
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="9001" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name:" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Steve" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Profession:" />
<EditText
android:id="@+id/profession"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Computer Engineer" />
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Send to New Activity" />
</LinearLayout>
Si noti che il NewActivity accede le informazioni da un Bundle (dal nome infoBundle in questo caso) ..
L'oggetto è "ricostruito" a livello di codice come se fosse passata.
Nuova attività - NewActivity.java:
package com.stephendiniz.objectsharing;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NewActivity extends Activity
{
LinearLayout ll;
TextView id;
TextView name;
TextView profession;
private final static String TAG = "NewActivity";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle infoBundle = getIntent().getExtras();
Student s = new Student(infoBundle.getInt("extraStudentId"), infoBundle.getString("extraStudentName"), infoBundle.getString("extraStudentProfession"));
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
id = new TextView(this);
name = new TextView(this);
profession = new TextView(this);
id.setText("Student Id: " + s.getId() + "\n");
name.setText("Student Name: " + s.getName() + "\n");
profession.setText("Student Profession: " + s.getProfession() + "\n");
ll.addView(id);
ll.addView(name);
ll.addView(profession);
Button closeButton = new Button(this);
closeButton.setText("Close Activity");
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
ll.addView(closeButton);
setContentView(ll);
}
}
Non dimenticare di aggiungere la nuova attività al vostro manifesto!
Android Manifest - AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stephendiniz.objectsharing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidObjectSharingActivity"
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=".NewActivity"
android:label="New Activity" />
</application>
</manifest>
Ci scusiamo per il mix di XML e Programmatica script, mi sono reso conto dopo la prima attività che sarebbe un sacco di file per visualizzare così ho condensato il ultimo file, ma dovresti essere in grado di ottenere l'idea ..
Spero che questo aiuti!
+1, questo è anche il mio metodo di scelta. Molto flessibile in quanto puoi scrivere i tuoi getter e setter in una classe Application personalizzata. – Guillaume