

UI designers think of apps in terms of "screens", which is a pretty close analog to activities. Therefore, our testing strategy can benefit from understanding and incorporating activities.Īn Android app is not a single experience with a set beginning and end, it is a collection of activities which can be entered and left at will. Figure 2.9 shows the app chooser screen and result of showing the GooglePlex at zoom level 23.The Activity is a core concept of Android apps and fundamentally shapes app architecture and UI design. On some devices, the mapIntent may be handled by the Google Map app or the Google Earth app the user is given a choice of which app to use. LISTING 2.1 MainActivity.java Clicking a Buttonġ: Button activityButton = (Button) findViewById(R.id.button) 2: tOnClickListener(new View.OnClickListener() ) It is easy to look at code as setting an OnClickListener for the activityButton. That seems complicated for this snippet of code in Listing 2.1! There is a lot going on. To implement an OnClickListener, the onClick() method must be defined and overridden. The View.OnClickListener is defined inline in the code. A View.OnClickListener() object is passed as a parameter. The setOnClickListener() method for activityButton is called on line 2. The code in Listing 2.1 defines a Button ( ) and adds a listener.Ī button named activityButton is defined in line 1. To detect that the button is clicked, you add an onClickListener().

You can think of the Android framework as having events that occur and a listener that listens for those events. The onCreate() method is used to define a Button and to specify an action to occur when the Button is clicked. The onCreate() method runs when MainActivity is created in the app. The generated code includes a method called onCreate(). MainActivity.java was generated with your project. The next step is to add code to MainActivity to detect when the button is clicked and to start SecondaryActivity. You have used Android Studio to create the foundation for a simple application in which MainActivity will start SecondaryActivity. You have learned about Intents and Activities.
