+ All Categories
Home > Documents > Pour ompléter l’introdution -...

Pour ompléter l’introdution -...

Date post: 05-Sep-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
28
Projet de Licence - 2014-2015 Pour compléter l’introduction
Transcript
Page 1: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Pour compléter l’introduction

Page 2: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 02 / 28

• Ajouts

• Retirer

• Par FragmentTransaction (et commit)

• Déplacement par tri des View générées

Fragments Menu Intents

Page 3: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 03 / 28

• Il s’agit de déplacer la vue liée au fragment

• Il faut donc le retrouver dans son parent, et le changer d’indice

– Boucle for…

– Car il manque :

private View[] getChilds(ViewGroup v) {

View [] res = null;

if (v != null) {

res = new View[v.getChildCount()];

for(int i = 0; i < res.length; i++) res[i] = v.getChildAt(i);

}

return res;

}

• Pour changer l’indice, c’est la méthode <ViewGroup>.addView( v, i)… mais avant il faut l’enlever

Fragments Menu Intents

Page 4: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 04 / 28

• Caution: ”Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).”http://developer.android.com/training/basics/activity-lifecycle/recreating.html

• Re-créer => refaire des objets…• Pour les fragments, on s’en sort car il y a le paramètres (et recréer avec ce même

paramètre)• Des fonctions peuvent être appelées avant destruction

– onSaveInstanceState pour une Activité– onSaveInstanceState pour un fragment– on peut y remplir le bundle– Ne pas oublier l’appel à super.xxx

• Méthode pour utiliser ce qui est sauvé– onCreate(Bundle saved) pour un fragment– onRestoreInstanceState(Bundle inState) pour une activité

• A faire évoluer… en même temps que le code…

Fragments Menu Intents

Page 5: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 05 / 28

• Le tri des tâches : – Comparable de java

– Enum de java

• C.f. cours POO s5…

• Puis gestion de la rotation (re remplir la liste de fragment, à partir de leur onCreate par un appel à leur activité)

Fragments Menu Intents

Page 6: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 06 / 28

• Source de l’image : « android programming, the big nerd ranch guide »

Fragments Menu Intents

Page 7: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 07 / 28

• Appel Méthode setRetainInstance(true) dans onCreate du Fragment

android programming, the big nerd ranch guide

Fragments Menu Intents

Page 8: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 08 / 28

• L’attribut android:name attribute indique la classe qui extends Fragment

• Insertion de la Viewretournéepar onCreateView()

• Note: chaque fragment nécessite un id (ou tag) – Meilleur gestion par Android

– android:id attribute (unique)

– android:tag avec une string unique

– Les deux

Une View “fragment”

<fragmentandroid:name=“nom_complet_classe"android:id="@+id/le_id"etc.

" />

Fragments Menu Intents

Page 9: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 09 / 28

• Comme un fragment…

– Un peu particulier (dialog)

– Méthode show, pas de transaction

• Besoin de prévoir la communication (interface logicielle), notamment pour la valeur de retour

Fragments Menu Intents

Page 10: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 10 / 28

Fragments Menu Intents

Page 11: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 11 / 28

• Dossier menu dans res<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<itemandroid:id="@+id/tri_buttoir"android:orderInCategory="100"android:showAsAction="never"

android:title="@string/tri_buttoir"/>

<itemandroid:id="@+id/tri_creation"android:orderInCategory="100"android:showAsAction="never"

android:title="@string/tri_creation"/></menu>

Fragments Menu Intents

Page 12: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 12 / 28

• <menu>Defines a Menu, which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.

• <item>Creates a MenuItem, which represents a single item in a menu. This element may contain a nested<menu> element in order to create a submenu.

• <group>An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility. For more information, see the section about Creating Menu Groups.

• Pour la balise item :• android:id

A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it.

• android:iconA reference to a drawable to use as the item's icon.

• android:titleA reference to a string to use as the item's title.

• android:showAsActionSpecifies when and how this item should appear as an action item in the action bar.

http://developer.android.com/guide/topics/ui/menus.htmlFragments Menu Intents

Page 13: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 13 / 28

• Méthode pour la création dans une Activity@Overridepublic boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.nom_du_fichier_xml,

menu);return true;

}

Fragments Menu Intents

Page 14: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 14 / 28

• Toujours dans une Activity@Overridepublic boolean onOptionsItemSelected(MenuItem item) {

// Handle item selectionswitch (item.getItemId()) {

case R.id.un_id:// des actions…return true;

case R.id.un_autre_id:// des actions en réponse à cet item làreturn true;

// et ainsi de suitedefault:

return super.onOptionsItemSelected(item);}

}

Fragments Menu Intents

Page 15: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 15 / 28

• Le tri des tâches :

– Comparable de java

– Enum de java

• C.f. cours POO s5…

• Puis gestion de la rotation (re remplir la liste de fragment, à partir de leur onCreatepar un appel à leur activité)

// pour alterner les tris

ComparerDate dernierTri = null;

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.tri_buttoir:

if ((dernierTri != null) && (dernierTri.equals(ComparerDate.BUTTOIR_CROISSANT))) dernierTri = ComparerDate.BUTTOIR_DECROISSANT;

else dernierTri = ComparerDate.BUTTOIR_CROISSANT;

break;

case R.id.tri_creation:

if ((dernierTri != null) && (dernierTri.equals(ComparerDate.CREATION_CROISSANT))) dernierTri = ComparerDate.CREATION_DECROISSANT;

else dernierTri = ComparerDate.CREATION_CROISSANT;

break;

default:

return super.onOptionsItemSelected(item);

}

trier(dernierTri);

return true; }

Fragments Menu Intents

Page 16: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 16 / 28

V1 : pas de menu

V2 : un menu « simple »

V3 : aller dans une activité fille

V4 : utiliser une activité (Intent) prédéfini

Fragments Menu Intents

Page 17: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 17 / 28

• Fisheye, exemple de personnalisation d’une View– Intégration dans le xml

<fisheye.four.image.DeformablePolygons [...] />

– Surcharge de onDraw, utilisation de Paint

• Mise en place d’un menu « simple »– inflate du menu dans

onCreateOptionsMenu– onOptionsItemSelected pour

réagir aux actions– Utilisation de invalidate() sur la

vue pour la mettre à jouir

Fragments Menu Intents

Page 18: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 18 / 28

• Autre écran = autre activité• Attention au cycle de vie (détruit, pas détruit ?)

• Création d’une autre activité• Déclaration dans le manifest (après l’activité principale)<activity

android:name="fisheye.four.SettingsActivity"android:label="@string/title_activity_settings“android:parentActivityName="fisheye.four.Fisheye">

<meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value="fisheye.four.Fisheye" />

</activity>

Fragments Menu Intents

Page 19: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 19 / 28

• Méthode simple : startActivity(Intent i) dans Activity

• Intent– Objet de communication avec l’OS

– Dans ce cas, constructeur new Intent(Context, Class)• Context = activité qui lance

• Class = le type de l’activité à lancer

– putExtra pour ajouter des données

• Dans le onCreate de la nouvelle activité– getIntent( )

– getIntent().getIntExtra(“clef", val_defaut);

Fragments Menu Intents

Page 20: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 20 / 28

• Lancement avec startActivityForResult• Méthode setResult(code, intent)

– Avec un code de retour– Avec un Intent (et des extra)

• Méthode de retour dans l’activité qui a lancé

protected void onActivityResult(int requestcode, intresultcode, Intent data) {

if (data == null) return ;Date d = (Date)

data.getSerializableExtra("date");// etc.

}

Fragments Menu Intents

Page 21: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 21 / 28

android programming, the big nerd ranch guide

APPLI

Extra, etc.

Activité A

Activité B

Extra, etc.

Fragments Menu Intents

android programming, the big nerd ranch guide

Page 22: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 22 / 28

Affichage du bouton (dans la barre)if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

getActionBar().setDisplayHomeAsUpEnabled(true);

}

Récupérer l’eventpublic boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case android.R.id.home: // etc. } return true; }

Fragments Menu Intents

Page 23: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 23 / 28

• Appel à setResults

– Faire l’Intent de retour (putExtra pour les valeurs de retour)

– Quitter la vue avec finish

• Pas dans l’esprit

• Mais l’avantage de conserver l’instance de l’activité…

• Utilisation du « up »

– L’activité de départ repart quasiment de zéro

– Perte du onActivityResult

Fragments Menu Intents

Page 24: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 24 / 28

Voir : https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android

// pour « relancer » l’activité parent

Intent up = NavUtils.getParentActivityIntent(this);

// éventuellement des valeurs de retours comme extra

up.putExtra(...);

// retour à l’activité, qui repasse par onCreate, mais qui

// n’a pas été tuée (pas de onRestoreInstanceState)

NavUtils.navigateUpTo(this,up);

// s’il n’y a pas de paramètre

// NavUtils.navigateUpFromSameTask(this);

Fragments Menu Intents

Page 25: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 25 / 28

• http://developer.android.com/training/implementing-navigation/ancestral.html

if (NavUtils.shouldUpRecreateTask(this, up)) {// This activity is NOT part of this app's task, so create a new

task// when navigating up, with a synthesized back stack.TaskStackBuilder.create(this)

// Add all of this activity's parents to the back stack.addNextIntentWithParentStack(up)// Navigate up to the closest parent.startActivities();

} else {// This activity is part of this app's task, so simply// navigate up to the logical parent activity.NavUtils.navigateUpTo(this, up);

}

Fragments Menu Intents

Page 26: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 26 / 28

• Pour « tuer »– « Normalement réserver à l’OS »– finish() (+System.exit(0) )– Etc.

• Pour relancer l’application au début à partir de là où on est :– Dans le manifest, pour L’activité ciblée

android:launchMode="singleTop“– Depuis l’endroit où on veut revenir « au début »Intent intent = new Intent(this, LActivitéPrincipale.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

Fragments Menu Intents

Page 27: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 27 / 28

• Reconnaissance de parole de google

• Intégrée à Android

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

/**

* Fire an intent to start the speech recognition activity.

*/

@Override

public void onClick(View v) {

// lancement de l'intent avec le nom de la classe... (constante)

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

// paramètre requis pour interprétation du résultat...

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

// titre

intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Fisheye - Speech recognition");

// VOICE_RECOGNITION_REQUEST_CODE pour retrouver l'appel pour le résultat

// nombre choisi par le développeur

startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

}

Fragments Menu Intents

Page 28: Pour ompléter l’introdution - unice.frdeptinfo.unice.fr/.../PL-s6-PRG-03-Android-Fragement.pdf · 2015. 2. 19. · onCreate du Fragment android programming, the big nerd ranch

Pro

jet de Licen

ce -2

01

4-2

01

5

Philippe Renevier Gonin - L3 Informatique - Projet de Licence - Fragment, Menu, Intents - Semestre 6, 2014-2015 28 / 28

protected void onActivityResult(int requestcode, int resultcode, Intent data) {// test si le retour est ok et si le retours vient bien de le reco vocale

if (requestcode == VOICE_RECOGNITION_REQUEST_CODE && resultcode == RESULT_OK) {

ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

// […]}else {// un autre cas…}

}

Fragments Menu Intents


Recommended