Вы находитесь на странице: 1из 29

Lecture 4

HANDLING RUNTIME CHANGES AND INTENTS

Handling Runtime Changes


Saving State Using onSaveInstanceState & onRestoreInstanceState

Retain an object during a configuration change


Using onRetainNonConfigurationInstance() & getLastNonConfigurationInstance() Handle the configuration change yourself Prevent the system from restarting your Activity

Method 1 : Saving State

Saving Application State : Save


protected void onSaveInstanceState (Bundle outState) {

// TODO Auto-generated method stub


super.onSaveInstanceState(outState); outState.putInt("color", Color.GREEN); }

Saving Application State Restore


@Override protected void onRestoreInstanceState(Bundle savedInstanceState)

{
// TODO Auto-generated method stub super.onRestoreInstanceState(savedInstanceState); int color = savedInstanceState.getInt("color"); //myWidget.setBackgroundColor(color); }

Saving State and Bundles


Overriding the onSaveInstanceState and onRestoreInstanceState save and restore the application state after restart.

E.g. when the screen rotates. Not when using the back key

Saving State Notes


Called by default for all widgets Text in EditText and CheckBox state are saved automatically but you have to make them an ID

you should always call the superclass implementation


You should NOT use saveInstanceState for persistence purposes

Method 2 : Retain an object

Retaining a stateful Object


Implement onRetainNonConfigurationInstance() to return an object carrying your data and then retrieve the data

when your Activity starts again with getLastNonConfigurationInstance()


For example:

Method 3 : Handling configurations changes by Android

Handling configurations changes by Android


You can stop activity from being restarted by adding the following in the manifeast

Handing runtime changes reference


http://static.meizu.com/m9sdk/guide/topics/resources/runtime-changes.html

Intents

What is an intent
An intent is an abstract description of an operation to be performed It can be used to

Start another activity, using startActivity()


Broadcast a message to interested broadcast receivers, using sendBroadCast() Starting a service, using startService() or bindService()

Intents: starting another activity


Intent myAct2 = new Intent(this,MyActivity2.class); startActivity(myAct2);

The manifest file needs to know about the activity


Add this entry inside the application element

Implicit and explicit intents


Explicit intents designate the target component by its name

Implicit intents
do not name a target. Implicit intents are often used to activate components in other applications.

Intent Structure
Action -- The general action to be performed, such as ACTION_VIEW

ACTION_EDIT
ACTION_MAIN Data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri. E.g. Open contact from contacts named John Steve Intent with ACTION_VIEW and data name of contact

Intent Example : View Contact(s)

Examples of Data/Action Pairs

Complete list of actions here :


http://developer.android.com/reference/android/content/Intent.html

Exercise
Make an application that takes a phone number as input and click a button to call. Call immediately, do not show the dialer

Intents Secondary Attributes (cont.)


Category Categories provide additional detail about the action the intent is perform When resolving an intent, only activities that provide all of the requested categories will be used. Components Components classes (optional). explicit name of a component class to handle the intent

Types
Types of data/message related to the intents as we saw in previous examples Extras Extra information needed for handling the intent by the intent receiver

Intents Examples
Some times the action/data pairs are not enough information to the activity. E.g. sending SMS Action : SendSMS

Data : to whom
What about the message itself ? Thats why there are Secondary attributes.

Intents Examples (cont.)


Show images

Intents Examples (cont.)


Launching Music Player

Intents Examples (cont.)


Play Music File

Intents Examples (cont.)


Sending MMS

Intents Examples (cont.)


Sending Mails Intent sendMail = new Intent(Intent.ACTION_SEND);

Assignment : Email Sender

Вам также может понравиться