1 / 20

Programming with Android: Activities

Programming with Android: Activities. Luca Bedogni Marco Di Felice Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna. Activity. Outline : What is started by the device It contains the application's informations Has methoda to answer certain events

jdunning
Download Presentation

Programming with Android: Activities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Android: Activities Luca Bedogni Marco Di Felice Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

  2. Activity 2 • Outline: • Whatisstarted by the device • Itcontains the application'sinformations • Hasmethoda to answercertainevents • An applicationcould be composed of multiple activities

  3. Creating an activity 3 • Create a class that is a subclass of Activity • Implement callback methods • OnCreate(): • Initialize • SetContentView()

  4. Activity lifecycle 4

  5. Activity lifecycle 5

  6. Activities 6 • Need to implementevery single method? No! • Itdepends on the applicationcomplexity • Whyitisimportant to understand the activitylifecycle? • So yourapplicationdoesnot crash (or do funnythings) while the userisrunningsomething else on the smartphone • So yourapplicationdoesnotconsumeunnecessaryresources • So the user can safely stop yourapplication and return to itlater

  7. Activitiesstates 7 • Resumed • The activityis in the foreground, and the user can interact. • Paused • The activityispartiallyoverlayed by anotheractivity. Cannotexecuteany code norreceiveinputs. • Stopped • Activity ishidden, in the background. Itcannotexecuteany code.

  8. Activity lifecycle 8 • OnCreate() • Called when the activity is created • Should contain the initialization operations • Has a Bundle parameter • If onCreate() succesfull terminates, it calls onStart()

  9. Activity lifecycle 9 • OnStart() • Called when onCreate() terminates • Called right before it is visible to user • If it has the focus, then onResume() is called • If not, onStop() is called

  10. Activity lifecycle 10 • OnResume() • Called when the activity is ready to get input from users • Called when the activity is resumed too • If it succesfully terminates, then the Activity is RUNNING

  11. Activity lifecycle 11 • OnPause() • Called when another activity comes to the foreground, or when someone presses back • Commit unsaved changes to persistent data • Stop cpu-consuming processes • Make it fast

  12. Activity lifecycle 12 • OnRestart() • Similar to onCreate() • We have an activity that was previously stopped

  13. Activity lifecycle 13 • OnStop() • Activity is no longer visible to the user • Could be called because: • the activity is about to be destroyed • another activity comes to the foreground

  14. 14 Activity lifecycle OnDestroy() The activity is about to be destroyed Could happen because: The systems need some stack space Someone called finish() method on this activity Could check with isFinishing()

  15. Activity loops 15 • Mainly 3 different loops • Entire lifetime • Between onCreate() and onDestroy(). • Setup of global state in onCreate() • Release remaining resources in onDestroy() • Visible lifetime • Between onStart() and onStop(). • Maintain resources that has to be shown to the user. • Foreground lifetime • Between onResume() and onPause(). • Code should be light.

  16. Activitiesin the manifest 16 • Declare them before running them • <activityandroid:name=".MainActivity"android:label="@string/app_name"> • <intent-filter> • <actionandroid:name="android.intent.action.MAIN"/> • <categoryandroid:name="android.intent.category.LAUNCHER"/> • </intent-filter> • </activity> • Why “MAIN” and “LAUNCHER”? • To show the application in the menu

  17. RecreatingActivities 17

  18. RecreatingActivities 18 • Android keeps the state of each view • Remember to assign unique Ids to them • So, no code is needed for the “basic” behavior • What if I want to save more data? • Override onSaveInstanceState() and onRestoreInstanceState() • staticfinalString STATE_SCORE ="playerScore"; • @Override • publicvoid onSaveInstanceState(Bundle savedInstanceState){ •     savedInstanceState.putInt(STATE_SCORE, mCurrentScore); • super.onSaveInstanceState(savedInstanceState); • }

  19. RecreatingActivities 19 • @Override • protectedvoid onCreate(Bundle savedInstanceState){ • super.onCreate(savedInstanceState);// Always call the superclass first • if(savedInstanceState !=null){ • // Restore value of members from saved state •         mCurrentScore = savedInstanceState.getInt(STATE_SCORE); • }else{ • // Probably initialize members with default values for a new instance • } • } • publicvoid onRestoreInstanceState(Bundle savedInstanceState){ • super.onRestoreInstanceState(savedInstanceState); •     mCurrentScore = savedInstanceState.getInt(STATE_SCORE); • }

  20. Activity: Conclusions 20 • Activities should be declared in the Manifest • Extend the Activity class • Code wisely • Put your code in the right place • Optimize it • Test even on low-end devices

More Related