SlideShare a Scribd company logo
1 of 14
Android Application Lifecycle
and Menus

•
•

Except as otherwise noted, the content of this presentation is
licensed under the Creative Commons Attribution 2.5 License.
l

l

Application Lifecycle

See flowchart in
http://developer.android.com/guide/topics/fundamentals.html

l

Active lifetime
l
l

l

Visible lifetime
l
l

l

has focus, accepting UI events
onResume to onPause
Visible, but does not have focus
onStart to onStop

Full lifetime
l

onCreate to onDestroy
l
l

Active Lifetime Example

Campus Maps App
l Shows user location on campus map image
l http://market.android.com/search?q=pname:com.simexusa.campusmaps_full
l

Active Lifetime Example

Campus Maps
Turn on/off GPS to save battery when UI not in focus
•
•
•
•
•
•
•
•
•
•
•
•
•

@Override
protected void onPause() {
super.onPause();
//stop receiving GPS and Orientation data
locationManager.removeUpdates(locationListener);
}
@Override
protected void onResume() {
super.onResume();
//restart receiving GPS and Orientation data
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
l
l

Visible Lifetime Example

Campus Maps
l Save state as app could get killed after onStop()
@Override
protected void onStop() {
super.onStop();
savePreferences();
}
@Override
protected void onStart() {
super.onStart();
restoreUIState();
}
l
l

Visible Lifetime Example

Campus Maps
l Save state as app could get killed after onStop()
•
•
•
•
•
•
•
•
•
•
•
•
•
•

private void savePreferences() {
SharedPreferences cmSharedPreferences =
getSharedPreferences(CMPREFS,Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = cmSharedPreferences.edit();
editor.putBoolean(VIRTUAL_MODE, inVirtualMode);
editor.putInt(MAP_INDEX, curMapIndex);
editor.commit();
}
private void restoreUIState() {
SharedPreferences cmSharedPreferences =
getSharedPreferences(CMPREFS,Activity.MODE_PRIVATE);
inVirtualMode = cmSharedPreferences.getBoolean(VIRTUAL_MODE, true);
curMapIndex = cmSharedPreferences.getInt(MAP_INDEX, 0);
}
l
l

l

UI Elements

View
l Control
l ViewGroup
l Layout
l Widget (Compound Control)
Many expected Views
l Button, CheckBox, RadioButton
l TextView, EditText, ListView
l Can be customized by extending and overriding onDraw()
l

l
l

XML UI Configuration

Layouts can specify UI elements (provided and custom)
res/layout
l

l

Campus Maps Example

See res/layout/main.xml

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
l
l

Custom View Example

Campus Maps
• public class CampusMapView extends View {
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (image != null) {
drawMapWithCircle(canvas);
drawCompass(canvas);
}
}
private void drawMapWithCircle(Canvas canvas) {
image.draw(canvas); //…
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.myLocationOuter));
canvas.drawCircle(circleX, circleY, 10, paint); //…
}
};
l

l
l
l

Menus

Icon Menu (up to 6 icons)
Expanded Menu (from More on Icon Menu)
Submenus
l
l

•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

Static Menu Example

Campus Maps : onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//Help menu
menu.add(0, MENU_HELP, Menu.NONE, R.string.menu_help);
//Virtual toggle menu
menu.add(0, MENU_VIRTUAL, Menu.NONE, R.string.menu_item_virtual_off);
if (VERSION==VERSION_SINGLE) {
return true; // do no more
}
//Choose favorite menu
SubMenu changeMapMenu = menu.addSubMenu(0, MENU_CHANGE_MAP, Menu.NONE,
R.string.menu_change_map);
changeMapMenu.add(0, MENU_CHANGE_0, 0, favCampusMaps[0].title);
changeMapMenu.add(0, MENU_CHANGE_1, 1, favCampusMaps[1].title);
changeMapMenu.add(0, MENU_CHANGE_2, 2, favCampusMaps[2].title);
changeMapMenu.add(0, MENU_CHANGE_3, 3, favCampusMaps[3].title);
changeMapMenu.add(0, MENU_CHANGE_4, 4, favCampusMaps[4].title);
return true;
}
l
l

Handle Menu Selection

Campus Maps : onOptionsItemSelected

• @Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_HELP: {
Toast.makeText(CampusMapsActivity.this,
If you don't see a map, check your internet connectivity (mobile data or wifi).",
Toast.LENGTH_LONG).show();
return true;
}
case MENU_VIRTUAL: {
if (VERSION == VERSION_TRIAL) {
Toast.makeText(CampusMapsActivity.this, "Only virtual mode available in trial version.",
Toast.LENGTH_LONG).show();
} else {
inVirtualMode = !inVirtualMode; //toggle
setupLocationService();
}
return true; }
l

Dynamic Menu Example

Campus Maps : onPrepareOptionsMenu
• @Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem mi = null;
mi = menu.findItem(MENU_VIRTUAL);
if (inVirtualMode)
mi.setTitle(R.string.menu_item_virtual_off);
else
mi.setTitle(R.string.menu_item_virtual_on);
if (VERSION==VERSION_SINGLE) {
return true; // do no more
}
if (newMapAdded) {
menu.setGroupVisible(0, false);
menu.setGroupVisible(1, true);
}
else {
menu.setGroupVisible(0, true);
menu.setGroupVisible(1, false);
}

More Related Content

Similar to Application lifecycle

Titanium appcelerator best practices
Titanium appcelerator best practicesTitanium appcelerator best practices
Titanium appcelerator best practicesAlessio Ricco
 
Aplicações assíncronas no Android com Coroutines & Jetpack
Aplicações assíncronas no Android com Coroutines & JetpackAplicações assíncronas no Android com Coroutines & Jetpack
Aplicações assíncronas no Android com Coroutines & JetpackNelson Glauber Leal
 
Android Lab Test : Using the camera preview (english)
Android Lab Test : Using the camera preview (english)Android Lab Test : Using the camera preview (english)
Android Lab Test : Using the camera preview (english)Bruno Delb
 
Open Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 TutorialOpen Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 Tutorialantiw
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdfatozshoppe
 
Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views Lars Vogel
 
Android webinar class_4
Android webinar class_4Android webinar class_4
Android webinar class_4Edureka!
 
Introdução ao RecyclerView
Introdução ao RecyclerViewIntrodução ao RecyclerView
Introdução ao RecyclerViewRodrigo Amora
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworksTomáš Kypta
 
How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabScilab
 
Lhy tutorial gui(1)
Lhy tutorial gui(1)Lhy tutorial gui(1)
Lhy tutorial gui(1)Brijesh Naik
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#Daniel Fisher
 
Developing Multi Platform Games using PlayN and TriplePlay Framework
Developing Multi Platform Games using PlayN and TriplePlay FrameworkDeveloping Multi Platform Games using PlayN and TriplePlay Framework
Developing Multi Platform Games using PlayN and TriplePlay FrameworkCsaba Toth
 
Enhancing UI/UX using Java animations
Enhancing UI/UX using Java animationsEnhancing UI/UX using Java animations
Enhancing UI/UX using Java animationsNaman Dwivedi
 
Rotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billyRotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billynimbleltd
 
Embracing the Lollipop
Embracing the LollipopEmbracing the Lollipop
Embracing the LollipopSonja Kesic
 

Similar to Application lifecycle (20)

Titanium appcelerator best practices
Titanium appcelerator best practicesTitanium appcelerator best practices
Titanium appcelerator best practices
 
Aplicações assíncronas no Android com Coroutines & Jetpack
Aplicações assíncronas no Android com Coroutines & JetpackAplicações assíncronas no Android com Coroutines & Jetpack
Aplicações assíncronas no Android com Coroutines & Jetpack
 
Android Lab Test : Using the camera preview (english)
Android Lab Test : Using the camera preview (english)Android Lab Test : Using the camera preview (english)
Android Lab Test : Using the camera preview (english)
 
Open Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 TutorialOpen Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 Tutorial
 
Map
MapMap
Map
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
 
Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views
 
Android webinar class_4
Android webinar class_4Android webinar class_4
Android webinar class_4
 
Introdução ao RecyclerView
Introdução ao RecyclerViewIntrodução ao RecyclerView
Introdução ao RecyclerView
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
Rcp by example
Rcp by exampleRcp by example
Rcp by example
 
pebble - Building apps on pebble
pebble - Building apps on pebblepebble - Building apps on pebble
pebble - Building apps on pebble
 
How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in Scilab
 
Lhy tutorial gui(1)
Lhy tutorial gui(1)Lhy tutorial gui(1)
Lhy tutorial gui(1)
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
 
Developing Multi Platform Games using PlayN and TriplePlay Framework
Developing Multi Platform Games using PlayN and TriplePlay FrameworkDeveloping Multi Platform Games using PlayN and TriplePlay Framework
Developing Multi Platform Games using PlayN and TriplePlay Framework
 
Enhancing UI/UX using Java animations
Enhancing UI/UX using Java animationsEnhancing UI/UX using Java animations
Enhancing UI/UX using Java animations
 
Rotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billyRotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billy
 
Embracing the Lollipop
Embracing the LollipopEmbracing the Lollipop
Embracing the Lollipop
 
Duel of Two Libraries: Cairo & Skia
Duel of Two Libraries: Cairo & SkiaDuel of Two Libraries: Cairo & Skia
Duel of Two Libraries: Cairo & Skia
 

More from Training Guide

More from Training Guide (7)

Persistences
PersistencesPersistences
Persistences
 
Theads services
Theads servicesTheads services
Theads services
 
App basic
App basicApp basic
App basic
 
Deployment
DeploymentDeployment
Deployment
 
Getting started
Getting startedGetting started
Getting started
 
Intents broadcastreceivers
Intents broadcastreceiversIntents broadcastreceivers
Intents broadcastreceivers
 
Tdd
TddTdd
Tdd
 

Recently uploaded

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Application lifecycle

  • 1. Android Application Lifecycle and Menus • • Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
  • 2. l l Application Lifecycle See flowchart in http://developer.android.com/guide/topics/fundamentals.html l Active lifetime l l l Visible lifetime l l l has focus, accepting UI events onResume to onPause Visible, but does not have focus onStart to onStop Full lifetime l onCreate to onDestroy
  • 3. l l Active Lifetime Example Campus Maps App l Shows user location on campus map image l http://market.android.com/search?q=pname:com.simexusa.campusmaps_full
  • 4. l Active Lifetime Example Campus Maps Turn on/off GPS to save battery when UI not in focus • • • • • • • • • • • • • @Override protected void onPause() { super.onPause(); //stop receiving GPS and Orientation data locationManager.removeUpdates(locationListener); } @Override protected void onResume() { super.onResume(); //restart receiving GPS and Orientation data locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); }
  • 5. l l Visible Lifetime Example Campus Maps l Save state as app could get killed after onStop() @Override protected void onStop() { super.onStop(); savePreferences(); } @Override protected void onStart() { super.onStart(); restoreUIState(); }
  • 6. l l Visible Lifetime Example Campus Maps l Save state as app could get killed after onStop() • • • • • • • • • • • • • • private void savePreferences() { SharedPreferences cmSharedPreferences = getSharedPreferences(CMPREFS,Activity.MODE_PRIVATE); SharedPreferences.Editor editor = cmSharedPreferences.edit(); editor.putBoolean(VIRTUAL_MODE, inVirtualMode); editor.putInt(MAP_INDEX, curMapIndex); editor.commit(); } private void restoreUIState() { SharedPreferences cmSharedPreferences = getSharedPreferences(CMPREFS,Activity.MODE_PRIVATE); inVirtualMode = cmSharedPreferences.getBoolean(VIRTUAL_MODE, true); curMapIndex = cmSharedPreferences.getInt(MAP_INDEX, 0); }
  • 7. l l l UI Elements View l Control l ViewGroup l Layout l Widget (Compound Control) Many expected Views l Button, CheckBox, RadioButton l TextView, EditText, ListView l Can be customized by extending and overriding onDraw()
  • 8. l l l XML UI Configuration Layouts can specify UI elements (provided and custom) res/layout
  • 9. l l Campus Maps Example See res/layout/main.xml @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
  • 10. l l Custom View Example Campus Maps • public class CampusMapView extends View { @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); if (image != null) { drawMapWithCircle(canvas); drawCompass(canvas); } } private void drawMapWithCircle(Canvas canvas) { image.draw(canvas); //… Paint paint = new Paint(); paint.setColor(getResources().getColor(R.color.myLocationOuter)); canvas.drawCircle(circleX, circleY, 10, paint); //… } };
  • 11. l l l l Menus Icon Menu (up to 6 icons) Expanded Menu (from More on Icon Menu) Submenus
  • 12. l l • • • • • • • • • • • • • • • • • • • Static Menu Example Campus Maps : onCreateOptionsMenu @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); //Help menu menu.add(0, MENU_HELP, Menu.NONE, R.string.menu_help); //Virtual toggle menu menu.add(0, MENU_VIRTUAL, Menu.NONE, R.string.menu_item_virtual_off); if (VERSION==VERSION_SINGLE) { return true; // do no more } //Choose favorite menu SubMenu changeMapMenu = menu.addSubMenu(0, MENU_CHANGE_MAP, Menu.NONE, R.string.menu_change_map); changeMapMenu.add(0, MENU_CHANGE_0, 0, favCampusMaps[0].title); changeMapMenu.add(0, MENU_CHANGE_1, 1, favCampusMaps[1].title); changeMapMenu.add(0, MENU_CHANGE_2, 2, favCampusMaps[2].title); changeMapMenu.add(0, MENU_CHANGE_3, 3, favCampusMaps[3].title); changeMapMenu.add(0, MENU_CHANGE_4, 4, favCampusMaps[4].title); return true; }
  • 13. l l Handle Menu Selection Campus Maps : onOptionsItemSelected • @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_HELP: { Toast.makeText(CampusMapsActivity.this, If you don't see a map, check your internet connectivity (mobile data or wifi).", Toast.LENGTH_LONG).show(); return true; } case MENU_VIRTUAL: { if (VERSION == VERSION_TRIAL) { Toast.makeText(CampusMapsActivity.this, "Only virtual mode available in trial version.", Toast.LENGTH_LONG).show(); } else { inVirtualMode = !inVirtualMode; //toggle setupLocationService(); } return true; }
  • 14. l Dynamic Menu Example Campus Maps : onPrepareOptionsMenu • @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); MenuItem mi = null; mi = menu.findItem(MENU_VIRTUAL); if (inVirtualMode) mi.setTitle(R.string.menu_item_virtual_off); else mi.setTitle(R.string.menu_item_virtual_on); if (VERSION==VERSION_SINGLE) { return true; // do no more } if (newMapAdded) { menu.setGroupVisible(0, false); menu.setGroupVisible(1, true); } else { menu.setGroupVisible(0, true); menu.setGroupVisible(1, false); }