Intro to Android

Activities

Activities are written in Java and registered in the manifest. They live in the package structure that you define in the manifest file. You can create subdirectories to better organize your files.

Code Example

public class LooselyCoupledActivity extends Activity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        setContentView( R.layout.activity_loosely_coupled );
    }
}

Activity's Role

Wherever possible, you should strive make your activities as dumb as possible, essentially an empty shell for your fragments. You'd also do work like configuring the action bar and menus here.

Ideally most of the business logic would be moved to Plain Old Java Objects (POJOs) for unit testing purposes, but in practice some of that logic ends up in the subviews or fragments.

Often Activities are used as an intermediary between components (e.g. Fragments) because you don't want them talking directly to each other. By using the listener pattern or a system bus, such as Otto, the components can post a message and the Activity will read it and respond. This keeps the concerns separate and easier to maintain the individual components.

Activity Results

You can start an activity inside a hosting activity and get results from that activity.

You use startActivityForResult() to start the activity. In the activity that was started, you use setResult() to set the result that will be returned. When back in the calling activity, you check the result code and respond appropriately.

A great example of this is invoking the system camera and then processing whether the picture-taking event was successful: http://developer.android.com/guide/topics/media/camera.html