TP1 – Résolution complète : Créer une
application avec deux activités et stockage
local
▪ Crée un nouveau projet (TP1_NomPrenom).
▪ Garde l’Empty Views Activity par défaut → elle s’appellera MainActivity.
▪ Ajoute une 2ᵉ activité : File ▸ New ▸ Activity ▸ Empty Views Activity → nomme-
la SecondActivity.
Android Studio ajoute normalement SecondActivity dans le [Link]. Si
besoin, vérifie qu’il y a bien:
<activity android:name=".SecondActivity" />
2) Interface de l’activité 1 (saisie + bouton)
res/layout/activity_main.xml
<LinearLayout xmlns:android="[Link]
android:orientation="vertical" android:gravity="center"
android:padding="24dp" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextNom"
android:hint="Entrez votre nom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"/>
<Button
android:id="@+id/buttonEnvoyer"
android:text="Valider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
</LinearLayout>
3) Code de l’activité 1 (Intent + SharedPreferences)
java/com/example/…/[Link]
package [Link].tp1; // adapte au package de ton projet
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText editTextNom;
private Button buttonEnvoyer;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextNom = findViewById([Link]);
buttonEnvoyer = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String nom = [Link]().toString().trim();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
// 1) N'écris dans les prefs que si l'utilisateur a vraiment saisi quelque chose
if (![Link]()) {
[Link]().putString("nom", nom).apply();
}
// 2) N'envoie l'extra que si non-vide (sinon, SecondActivity lira les prefs)
Intent i = new Intent([Link], [Link]);
if (![Link]()) {
[Link]("nom", nom);
}
startActivity(i);
}
});
}
}
4) Interface de l’activité 2 (affichage du message)
res/layout/activity_second.xml
<LinearLayout xmlns:android="[Link]
android:orientation="vertical" android:gravity="center"
android:padding="24dp" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewMessage"
android:text="Bonjour !"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
5) Code de l’activité 2 (récup Intent OU SharedPreferences)
java/com/example/…/[Link]
package [Link].tp1; // adapte au package
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends AppCompatActivity {
private TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
textViewMessage = findViewById([Link]);
// 1) Essayer de récupérer depuis l'Intent
String nom = getIntent().getStringExtra("nom");
// 2) Si vide, récupérer depuis SharedPreferences
if (nom == null || [Link]()) {
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
nom = [Link]("nom", "");
}
// 3) Affichage
if (nom == null || [Link]()) {
[Link]("Bonjour !");
} else {
[Link]("Bonjour " + nom + " !");
}
}
}
▪ Lance l’app, entre un nom, appuie Valider → SecondActivity affiche Bonjour
[Nom] !
▪ Force l’arrêt de l’app / relance-la → clique Valider sans écrire de nom :
▪ SecondActivity doit récupérer le nom sauvegardé en SharedPreferences.