Activity LifeCycle

An activity is a single entity that has the focus of the user and can be interacted by the user. Activities have 4 states: Active, Running, Paused, and Stopped.

  • If an activity is in the foreground of the screen (at the top of the activity stack), it is active, or running.
  • If an activity does not have focus but is still visible on the foreground, it is paused.
  • If an activity is not visible or in the background, it is stopped.
  • If an activity is paused or stopped, the system can drop the activity or kill the process to reallocate memory.
  • onCreate() is first called when creating an activity. This is commonly where all of the initial setup will be.
  • onStart() is when the activity is visible to the user.
  • onResume() is when the activity is interacted by the user.
  • onPause() is when the user chooses to return to a previous activity.
  • onStop() is when the activity is no longer visible to the user.
  • onDestroy() is called when the activity is finished and ready to be destroyed to release resources to be reallocated.

Learn More →

Intents

An intent is like a message that holds a set of instructions. They are often used to launch and link activities. Primarily, intents contain action and data. Actions are those that need to be executed, and data is usually the data that the actions operate on. Intents can additionally contain category, type, component, and extras.

There are two kinds of intents:

  • Explicit intents have a specified class that will handle the intent and run the instructions.
  • Implicit intents will be delegated based on the information it contains to which ever available component is best to run for the intent. These require more setup, such as specifying an intent filter in the AndroidManifest.xml.

Learn More →

Permissions

As a user, prior to Marshmallow (SDK 22-), you are prompted to give permissions on installation. For OS versions that are Marshmallow or newer (SDK 23+), permissions will be requested when it is needed based on the activity.

As a developer, permissions are not set by default. In order to use protected features of the device, you must include the associated permissions using <uses-permission> tags.

Learn More →

Best Practices

Readability vs creating objects

In Android, allocating objects is not free. (memory allocation adds up and eventually causes the OS to have to deallocate objects which means everything pauses for some length of time in the order of 10 - 50 millisec.) Of course, that does not mean to go so far towards reducing object allocation that the code becomes readable. However I think the way this is now is plenty readable, so if I were making the call I would avoid the extra objects.

Using Getters and Setters

In our app, we created a POJO (plain old Java object) with just getters and setters. This is a best practice that we observe because it will be useful in more complicated projects, as well as in unit testing. As of now, we have something simple like:

public String getPhoneNumber() {
    return mPhoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.mPhoneNumber = phoneNumber;
}

In the future, there may be more complicated logic involved in a getter or setter.

Debugging

To improve the quality of your app, use the logger to log messages to verify that your app is working as you expected. In the future, this allows other developers to understand what's going on in your code for better readability.

Additionally, when you're debugging your own code, you can use an IDE (such as IntelliJ or Eclipse) or even Google Chrome (depending on what you're making) to debug through your project. You can step through you code and find out where things are breaking.

UI Threads

The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread.

  • Do all UI work (visuals) on UI threads: for example, creating views, buttons, etc.
  • Do all other work on background thread or else the program can hang and crash.

Unit Tests

Unit tests are pieces of code you write that test small modular pieces (units) of your code to ensure they are working properly. They are vital to code maintenance during the development process. It is also often a good idea to write your code in a way to make them unit testable.