Book Image

Android Application Development Cookbook - Second Edition

By : Kyle Mew
Book Image

Android Application Development Cookbook - Second Edition

By: Kyle Mew

Overview of this book

The Android OS has the largest installation base of any operating system in the world; there has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community! This “cookbook” will make it easy for you to jump to a topic of interest and get what you need to implement the feature in your own application. If you are new to Android and learn best by “doing,” then this book will provide many topics of interest. Starting with the basics of Android development, we move on to more advanced concepts, and we’ll guide you through common tasks developers struggle to solve. The first few chapters cover the basics including Activities, Layouts, Widgets, and the Menu. From there, we cover fragments and data storage (including SQLite), device sensors, the camera, and GPS. Then we move on more advanced topics such as graphics and animation (including OpenGL), multi-threading with AsyncTask, and Internet functionality with Volley. We’ll also demonstrate Google Maps and Google Cloud Messaging (also known as Push Notifications) using the Google API Library. Finally, we’ll take a look at several online services designed especially for Android development. Take your application big-time with full Internet web services without having to become a server admin by leveraging the power of Backend as a Service (BaaS) providers.
Table of Contents (23 chapters)
Android Application Development Cookbook Second Edition
Credits
Disclaimer
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Understanding the activity lifecycle


The Android OS is a dangerous place for an activity. The demand for resources on a battery-operated platform is managed quite ruthlessly by the system. Our activities can be dumped from memory when it's running low, without even a moment's notice and along with any data they contain. Therefore, it is essential to understand the activity lifecycle.

The following diagram shows the stages through which an activity passes during its lifetime:

Along with the stages, the diagram also shows the methods that can be overridden. As you can see, we've already utilized most of these methods in the preceding recipes. Hopefully, getting the big picture will help in your understanding.

Getting ready

Create a new project in Android Studio with a Blank Activity, and call it ActivityLifecycle. We will use the (autogenerated) TextView method to display the state information.

How to do it...

To see the application move through the various stages, we will create methods for all the stages:

  1. Open activity_main.xml and add an ID to the autogenerated TextView:

    android:id="@+id/textViewState"
  2. The remaining steps will be in MainActivity.java. Add the following global declaration:

    private TextView mTextViewState;
  3. Modify the onCreate() method to save TextView and set the initial text:

    mTextViewState = (TextView)findViewById(R.id.textViewState);
    mTextViewState.setText("onCreate()\n");
  4. Add the following methods to handle the remaining events:

    @Override
    protected void onStart() {
        super.onStart();
        mTextViewState.append("onStart()\n");
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        mTextViewState.append("onResume()\n");
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mTextViewState.append("onPause()\n");
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        mTextViewState.append("onStop()\n");
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        mTextViewState.append("onRestart()\n");
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTextViewState.append("onDestroy()\n");
    }
  5. Run the application and observe what happens when the activity is interrupted by pressing the Back and Home keys. Try other actions, such as task switching, to see how they impact your application.

How it works...

Our activity can exist in one of these three states: active, paused, or stopped. There is also a fourth state, destroyed, but we can safely ignore it:

  • An activity is in the active state when its interface is available for the user. It persists from onResume() until onPause(), which is brought about when another activity comes to the foreground. If this new activity does not entirely obscure our activity, then ours will remain in the paused state until the new activity is finished or dismissed. It will then immediately call onResume() and continue.

  • When a newly started activity fills the screen or makes our activity invisible, then our activity will enter the stopped state, and the resumption will always invoke a call to onRestart().

  • When an activity is in either the paused or stopped state, the operating system can (and will) remove it from the memory when the memory is low or when other applications demand it.

  • It is worth noting that we never actually see the results of the onDestroy() method, as the activity is removed by this point. If you want to explore these methods further, then it is well worth employing Activity.isFinishing() to see whether the activity is really finishing before onDestroy() is executed, as seen in the following snippet:

    @Override
      public void onPause() {
      super.onPause();
      mTextView.append("onPause()\n ");
      if (isFinishing()){
        mTextView.append(" ... finishing");
      }
    }

Tip

When implementing these methods, always call the superclass before doing any work.

There's more...

Shutting down an activity

To shut down an activity, directly call its finish() method, which in turn calls onDestroy(). To perform the same action from a child activity, use finishFromChild(Activity child), where child is the calling subactivity.

It is often useful to know whether an activity is being shut down or merely paused, and the isFinishing(boolean) method returns a value that indicates which of these two states the activity is in.