Book Image

Android 3.0 Application Development Cookbook

By : Kyle Merrifield Mew
Book Image

Android 3.0 Application Development Cookbook

By: Kyle Merrifield Mew

Overview of this book

<p>Android is a mobile operating system that runs on a staggering number of smartphones and tablets. Android offers developers the ability to build extremely rich and innovative applications written using the Java programming language. Among the number of books that have been published on the topic, what&rsquo;s missing is a thoroughly practical, hands-on book that takes you straight to getting your job done without boring you with too much theory.<br /><br />Android 3.0 Application Development Cookbook will take you straight to the information you need to get your applications up and running. This book is written to provide you with the shortest possible route between an idea and a working application. <br /><br />Work through the book from start to finish to become an Android expert, or use it as a reference book by applying recipes directly to your project.<br /><br />This book covers every aspect of mobile app development, starting with major application components and screen layout and design, before moving on to how to manage sensors such as internal gyroscopes and near field communications. Towards the end, it delves into smartphone multimedia capabilities as well as graphics and animation, web access, and GPS. <br /><br />Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android 3.</p>
Table of Contents (18 chapters)
Android 3.0 Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Returning a result from an activity


Being able to start one activity from another is all well and good, but we will often need to know how the called activity has fared in its task or even which activity has been called. The startActivityForResult() method provides the most straightforward way to do this.

Getting ready

Returning a result from an activity is not that different from calling one the way we did in the previous recipe. Start up a new Android project in Eclipse and call it GettingResults.

How to do it...

In this recipe we will need to create a new activity class, provide it with an onCreate() method, then edit our default class and include our new activity in the manifest file:

  1. Create a new class called MyNewActivity in the same package as the GettingResults class and give it the Superclass android.app.Activity.

  2. Extend the class as an activity, provide it with an onCreate() method, and fill it out as given next.

  3. Pressing Ctrl + Space once you have typed as far as public void onCrea will prompt Eclipse to complete most of this method for you:

    public class MyNewActivity extends Activity {
    
      @Override
      public void onCreate(Bundle state) {
      super.onCreate(state);
    
        setResult(42);
        finish();
    
      }
    
    }
  4. Press Ctrl + Shift + O. This will import the following libraries:

    import android.app.Activity;
    import android.os.Bundle;
  5. Open the GettingResults class and edit it to look like this:

    public class GettingResults extends Activity {
    
      @Override
      public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.main);
    
        Intent i = new Intent(this, MyNewActivity.class);
        startActivityForResult(i, 0);
      }
    
      @Override
      protected void onActivityResult(int requestCode,
        int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, Integer.toString(resultCode),
          Toast.LENGTH_LONG).show();
      }
    
    }
  6. Import any library the class needs with Ctrl + Shift + O.

  7. Open the manifest file and include a new <activity> element underneath the one that is already there. Include the following attributes:

    <activity
      android:name=".MyNewActivity"
      android:label="my new activity">
    </activity>
  8. Run the application on a device or an emulator. A small pop-up appears in the initial activity that has been passed from the called one:

How it works...

Here, the called activity used setResult() to return a result code back to the calling activity. We used an arbitrary value in our example but a result code can be used to represent an outcome such as the index of a selected item.

The corresponding member in the calling activity is the onActivityResults() method. Besides the result code that we just sent from the called activity, the method receives a request code. This is simply the integer value that was passed with the startActivityForResult() call which takes the form:

startActivityForResult(Intent intent, int requestCode);

We used 0 as our request code because we knew where it came from—but this value can be used to identify where the request originated in less trivial applications with several activities.

Note

If startActivityForResult() is called with a negative request code it will act exactly as if it were a call to startActivity()that is, it will not return a result.

Activity results are always handled by the calling activity's onActivityResults() method, which makes use of the request code as well as the result code.

We made use of the Toast object which is a neat little pop-up view that can be used to unobtrusively inform the user of some event or the other. It also functions as a handy little tool for on-the-fly debugging as it doesn't need setting up or screen estate.

There's more...

In the previous example, we only returned an integer with setResult() but there is an alternative format.

Returning an intent with the result code

To return an intent along with the result code back to the calling activity, use:

setResult(int resultCode, Intent data);

Applied to the previous demonstration, the code would then look something like this:

Intent i = new Intent();
setResult(42, i);

finish();

See also

To learn more about creating new activity classes refer to the previous recipe, Switching between activities.

For more information on Toasts see the recipe Making a Toast in Chapter 7, Notifying the user.