Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Fundamentals

Orhan Obut
October 26, 2014

Android Fundamentals

Orhan Obut

October 26, 2014
Tweet

More Decks by Orhan Obut

Other Decks in Programming

Transcript

  1. Activity • Single, focused thing that the user can do.

    • Must have <activity> declaration in AndroidManifest.xml. • Lots of attributes to make configurations; name is mandatory • Any activity can be main entry. <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
  2. Launch modes How the activity should be launched into a

    task? • Standard • Single Top • Single Task • SingleInstance
  3. Intent Flags You can modify the default association of an

    activity to its task by including flags. • FLAG_ACTIVITY_NEW_TASK • FLAG_ACTIVITY_SINGLE_TOP • FLAG_ACTIVITY_CLEAR_TOP
  4. Activity Tips • Consider fragment for multitask • Consider support

    package • Lifecycle methods must always call the superclass implementation
  5. Fragments • Represents a behavior or a portion of user

    interface in an Activity. • Introduced in API level 11. • Use support library for API Level < 11 • Must be embedded in an activity • Fragment's lifecycle is directly affected by the host activity's lifecycle
  6. Fragments Tips • Use dynamic inflate • Make it loosely

    coupled • Do not communicate with another fragment directly. • Use callback listener for interaction with activity • Use static factory method for fragment creation
  7. Services • An application component that can perform long-running operations

    in the background • It does not provide a user interface • A service can essentialy take 2 forms : o Started o Bounded
  8. Started Services There are two classes you can extend to

    create a started service: • Service • IntentService startService(new Intent(this, MyService.class)); startService(new Intent(this, MyIntentService.class))
  9. Caution: A service runs in the main thread of its

    hosting process, the service : • Does not create its own thread • Does not run in a separate process (unless you specify otherwise) Intent service • Creates a worker thread • Stop itself
  10. Bounded Services • Allows application components to bind to it

    by calling bindService() in order to create a long-standing connection. • Use it when you want to interact with the service from activities and other components in your app • Use it when you expose some of your application's functionality to other applications, through IPC. Intent intent = new Intent(this, MyService.class) bindService(i, mServerConn, Context.BIND_AUTO_CREATE); startService(i);
  11. NOTE: Unlike the activity lifecycle callback methods, you are not

    required to call the superclass implementation of these callback methods.
  12. Intents • Intent is a communication object. • There are

    3 fundamental cases to use intents o Start an activity o Start a service o Deliver a broadcast • There are 2 type of intent : Explicit and Implicit
  13. Explicit Intents • Specify a component to start by name

    (the fully-qualified name) • Typically start a component in your own app For example : start an activity or service
  14. Implicit Intents • Do not name a specific component, but

    instead declare a general action to perform • Another app will handle the request • Verify that the intent will resolve to an activity For example: Open a web page if(sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent); }
  15. Pending Intent • Allows a foreign application to use your

    application's permissions to execute a predefined piece of code • Needs a regular intent Major cases : • Notifications • Widgets • AlarmManager Intent intent = new Intent(context, MainActivity.class); PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, FLAG); FLAG_ONE_SHOT FLAG_NO_CREATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
  16. Intent Filter • Structured description of Intent values to be

    matched • Often created in the android manifest, using intent-filter tags Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares. <activity android:name="ShareActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> </activity>
  17. Intents Tips • Always use an explicit intent when starting

    a Service and do not declare intent filters for your services for security concerns • If there are 2 same intent-filter, first match will consume.
  18. Simply respond to broadcast messages from other applications or from

    the system itself. • Normal broadcasts • Ordered broadcasts • Local broadcasts • Sticky broadcasts Broadcast Receiver
  19. Receiver Lifecycle • Only valid for the duration of the

    call to onReceive(Context, Intent). • Use NotificationManager to show information. • You may not bind to a service from within a BroadcastReceiver, instead use Context.startService()
  20. Process Lifecycle • A process that is currently executing a

    broadcast receiver (which holds onReceive()) is considered to be a foreground process. • It will be kept running by the system except under cases of extreme memory pressure. • After execution, the system may kill it
  21. Tips • Use LocalBroadcastManager, If you don't need to send

    broadcasts across applications. • Unregister it in Activity.onPause() if registering a receiver in your Activity. onResume() • Do not use Async operation onReceive() method, use Services if API level < 11 • You can use goAsync() method for Async operation after API Level 11