Passing data between activities & Debugging

Quick Review

  1. Which is the most popular Android version?
  2. What components exist in Android?
  3. What is an activity? What does an activity have?
  4. What are these - R, Android Manifest, app.gradle?
  5. What is the difference between .DEX and .APK?
  6. What intent types Android has?

Debugging & Logging in Android

What is debugging?

Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected. 

How is debugging working in Android?

Android Debug Bridge (ADB)

Server - :5555 - :5585

Deamon

Client

Using the System Log (1)

import android.util.Log;
...
public class MyActivity extends Activity {
    private static final String TAG = MyActivity.class.getSimpleName();
    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            Log.d(TAG, "onCreate() Restoring previous state");
            /* restore state */
        } else {
            Log.d(TAG, "onCreate() No saved state available");
            /* initialize app */
        }
    }
}

Using the System Log (2)

Log.<priority>(TAG, <string_to_log>)

  • V — Verbose (lowest priority)
  • D — Debug
  • I — Info
  • W — Warning
  • E — Error
  • F — Fatal
  • S — Silent (highest priority, on which nothing is ever printed)
  • WTFWhat a Terrible Failure

Using the System Log (3)

  1. Run your app
  2. Open the Android window - Alt + 6
  3. Choose the appropriate log level
  4. The window is called - Dalvik Debug Monitor Server

Let's try it!

Log the Activity lifecycle

Using breakpoints (1)

Breakpoints enable you to pause the execution of your app at a particular line of code, examine variables, evaluate expressions, and continue the execution line by line.

  1. Open the source file
  2. Locate the line where you want the execution to pause
  3. Left click next to the line in the grey space to put a breakpoint or press Ctrl + F8

Using breakpoints (2)

  • Evaluating expressions
  • Watching variable values
  • Step into, Step over, Resume

Let's try it!

Set a breakpoint at each lifecycle method

Bonus

"Hard work beats talent when talent doesn't work hard"

- Tim Notke

Break (10 mins)

Questions?

Passing data between activities

What can you do with an intent?

  1. Start your activities
  2. Call other activities for result
  3. Start services
  4. Send broadcast messages to the whole system

A message with which you can:

Types of intents

Explicit

Implicit

Intent i = new Intent(this, 
               MyActivity.class)
Intent i = new Intent("com.android.camera")

What has an intent?

  • Component name

  • Action - A string that specifies the generic action to perform (such as view or pick).

  • Data - Uri to the file

  • Category - the kind of component to handle the intent

  • Extras - additional data

  • Flags - metadata for the intent

How to handle an implicit intent?

Intent sendIntent = new Intent(Intent.ACTION_SEND);

String title = getResources().getString(R.string.chooser_title);
Intent chooser = Intent.createChooser(sendIntent, title);

if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}

Let's try it!

What is an intent-filter?

An expression in an app's manifest file that specifies the type of intents that the component would like to receive.

An intent-filter can have:

  • Action

  • Data

  • Category - category default is a must have if you want to receive intents

Pending Intents

  • Wrapper around Intent

  • Uses the contained intent as it is started from you app's process

  • Major use cases:

    • When the user taps on a notification

    • An action from Widget

    • To be started at a specific time

Action test

  • Intent-filter can have 0 or more actions
  • Intent's action must match the filter's
  • If the intent doesn't have and the filter doesn't have too it will pass

Category test

  • Every intent's category must match the filter's ones. The opposite is OK.
  • An intent with no categories will always pass the test

Data test

  • 0 or more data filters
  • Comparison by the declared data scheme
  • Comparison by MIME type

PackageManager is the class you can use to find the Activities that can fulfill your desired operation

Summary

  1. How to use Log.d/e/v ... ?

  2. How to use breakpoints and evaluate expressions?

  3. Activity Lifecycle

  4. What is an intent?

  5. What has an intent?

  6. Explicit and implicit intents

  7. What is an intent-filter?

  8. How is Android matching the intents?

  9. Pending intents

Thank you! :)

Made with Slides.com