Les code Flashcards
(4 cards)
Serializable Code
Activity 1 :
public class MainActivity … {
public void onCreate(…){ …
Button btn = (Button)
findViewById(R.id.BtAfficher);
btn.setOnClickListener(new
View.OnClickListener() {
public void onClick(View v) {
Intent itOut = new Intent(this,
Activity2.class);
Etudiant e1 = new Etudiant(…);
Bundle bd = new Bundle();
bd.putSerializable(“serializable, e1);
itOut.putExtras(bd);
startActivity(itOut);
…
}
Activity 2 :
public void onCreate(…){…// récupérer l’intentIntent itIn = getIntent();//extraire les données Bundle bd = itIn.getExtras();Etudiant e = new (Etudiant) bd.getSerializable(“serialzable”);
Bouton 1 : passer à Activity2 sans envoyer de données
Acitivity 1 :
public class MainActivity extends AppCompatActivity{@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bouton 1 : passer à Activity2 sans envoyer de donnéesIntent it1=new Intent(this,Activity2.class);
// Listener sur le bouton BtActivity21 :
Button btnAct21=(Button) findViewById(R.id.BtAct21);btnAct21.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// lancer l’Activity2startActivity(it1);
finish(); // si on veut fermer Activity1}
});
}
Acitivity 2 :
public class Activity2 extends AppCompatActivity{@Override
protected void onCreate(Bundle savedInstanceState){ …
}
public void onFermer2(View v){
// Retour des résultats
setResult(RESULT_OK);
finish(); // Fermer l’activité courante}
}
Bouton 2 : passer à Activity2 avec envoie de données
Acitivity 1 :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {…// Bouton 2 : passer à Activity2 avec envoie de donnéesIntent it2 = new Intent(this, Activity2.class);
Button btnAct22 = (Button) findViewById(R.id.BtAct22);btnAct22.setOnClickListener(new View.OnClickListener() {public void onClick(View v){// lire les données des EditTextEditText prenomText=(EditText)findViewById(R.id.prenomTXT);EditText nomText = (EditText) findViewById(R.id.nomTXT);String nom = nomText.getText().toString();
String prenom = prenomText.getText().toString();Bundle bd = new Bundle();
bd.putString(“nom”,nom);
bd.putString(“prenom”, prenom);
it2.putExtras(bd);
startActivity(it2); // lancer l’Activity2 avec les données} });
…}
Activity 2 :
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent itIn2 = getIntent();
Bundle bd = itIn2.getExtras();
String n = bd.getString(“nom”);
String p = bd.getString(“prenom”);
TextView txt = (TextView) findViewById(
R.id.msgActiv2);
txt.setText(“Bonjour “ + n + “ “+ p);
}
}
passer à Activity2 avec retour résultat
Acitivity 1 :
public class MainActivity extends AppCompatActivity {private ActivityResultLauncher<Intent> lancerA2;
Intent it3 = new Intent(this, Activity2.class);
@Override
protected void onCreate(Bundle savedInstanceState) { ...lancerA2 = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),this::onRetourA2);Button btnAct23 = (Button) findViewById(R.id.GoAct23);btnAct23.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {
// Bouton 3 : Récupérer les infos envoyées par l’Activité2EditText prenomT = (EditText) findViewById(R.id.prenomTXT);EditText nomT = (EditText) findViewById(R.id.nomTXT);String nom = nomT.getText().toString();
String prenom = prenomT.getText().toString();
Bundle bd = new Bundle();
bd.putString("nom",nom);
bd.putString("prenom", prenom);
it3.putExtras(bd);
lancerA2.launch(it3); } });
private void onRetourA2(ActivityResult res) {
if (res.getResultCode() == RESULT_CANCELED) {System.out.println("Echec de retour !!!");return;
}else
if(res.getResultCode() == RESULT_OK) {
Intent msg = res.getData();
Bundle bd = msg.getExtras();
String s = bd.getString("RES");
TextView resText=(TextView) findViewById(R.id.txtRes);resText.setText(s);
}
} }
...}
Activity 2 :
public class Activity2 extends AppCompatActivity{String resMsg=
"";
@Override
protected void onCreate(Bundle savedInstanceState) { ...Intent itIn = getIntent();
Bundle bd = itIn.getExtras();
String n = bd.getString("nom");
String p = bd.getString("prenom");
TextView txt = (TextView) findViewById(R.id.msgActiv2);txt.setText("Bienvenue " + n + " "+ p);
resMsg = "Au revoir : "+ n + " "+ p;
}
public void onFermer2(View v){
Intent resIt21 = new Intent();
// ajout des résultats
Bundle bd = new Bundle();
bd.putString("RES",resMsg);
resIt21.putExtras(bd);
setResult(RESULT_OK,resIt21);
finish();
} }</Intent>