Lecture 4 Handling Runtime Changes and Intents

Embed Size (px)

Citation preview

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    1/29

    Lecture 4HANDLING RUNTIME CHANGES AND INTENTS

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    2/29

    Handling Runtime Changes

    Saving StateUsing onSaveInstanceState & onRestoreInstanceState

    Retain an object during a configuration change

    Using onRetainNonConfigurationInstance() & getLastNonConfigurationInstance(

    Handle the configuration change yourself

    Prevent the system from restarting your Activity

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    3/29

    Method 1 : SavingState

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    4/29

    Saving Application State : Saveprotected void onSaveInstanceState (Bundle outState)

    {

    // TODO Auto-generated method stub

    super.onSaveInstanceState(outState);

    outState.putInt("color", Color.GREEN);

    }

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    5/29

    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);

    }

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    6/29

    Saving State and BundlesOverriding the onSaveInstanceState and onRestoreInstanceState

    save and restore the application state after restart.

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

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    7/29

    Saving State Notes Called by default for all widgets

    Text in EditText and CheckBox state are saved automatically but you have to make

    you should always call the superclass implementation

    You should NOT use saveInstanceState for persistence purposes

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    8/29

    Method 2 : Retain anobject

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    9/29

    Retaining a stateful ObjectImplement onRetainNonConfigurationInstance()

    to return an object carrying your data and then retrieve the data

    when your Activity starts again with getLastNonConfigurationInstance()

    For example:

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    10/29

    Method 3 : Handling configurations changesAndroid

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    11/29

    Handling configurations changes byAndroid

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

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    12/29

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

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    13/29

    Intents

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    14/29

    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()

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    15/29

    Intents: starting another activityIntent myAct2 = new Intent(this,MyActivity2.class);

    startActivity(myAct2);

    The manifest file needs to know about the activity

    Add this entry inside the application element

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    16/29

    Implicit and explicit intentsExplicit intents

    designate the target component by its name

    Implicit intents

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

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    17/29

    Intent StructureAction -- 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, ea Uri.

    E.g. Open contact from contacts named John Steve

    Intent with ACTION_VIEW and data name of contact

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    18/29

    Intent Example : View Contact(s)

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    19/29

    Examples of Data/Action Pairs

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    20/29

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    21/29

    ExerciseMake an application that takes a phone number as input and click a button to call.

    immediately, do not show the dialer

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    22/29

    Intents Secondary Attributes (coCategory

    Categories provide additional detail about the action the intent is perform

    When resolving an intent, only activities that provide all of the requested categoriused.

    Components

    Components classes (optional). explicit name of a component class to handle the

    TypesTypes 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

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    23/29

    Intents ExamplesSome times the action/data pairs are not enough information to the activity. E.g. s

    Action : SendSMS

    Data : to whom

    What about the message itself ?

    Thats why there are Secondary attributes.

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    24/29

    Intents Examples (cont.)Show images

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    25/29

    Intents Examples (cont.)Launching Music Player

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    26/29

    Intents Examples (cont.)Play Music File

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    27/29

    Intents Examples (cont.)Sending MMS

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    28/29

    Intents Examples (cont.)Sending Mails

    Intent sendMail = new Intent(Intent.ACTION_SEND);

  • 8/13/2019 Lecture 4 Handling Runtime Changes and Intents

    29/29

    Assignment : Email Sender