官术网_书友最值得收藏!

Chapter 2. Activities and Fragments - The Backbone of Your App

Activities and fragments are two of the most important concepts for an Android developer to master. In fact, in just about any Android-related programming interview, a common starting question is to ask a candidate to describe and outline activities, fragments, and their respective life cycles!

Broadly speaking, most Android apps consist of a series of connected screens that a user is able to navigate through. Obviously, more complex apps can be offered additional functionality such as background services, cloud messaging, broadcast receivers, and so on. However, the core UI will usually be centered around navigating through a series of connected activities or fragments nested within an activity.

A simple example of this basic UI in, for instance, a cooking-recipe application, would be a screen displaying a list of available recipes (Recipe List Activity) and another screen displaying details of each recipe (Recipe Detail Activity). These screens can be mapped easily to activities and managed by the system as an activity stack. In the earlier example, if a user navigates from the recipe list activity (by clicking on a recipe in the list), a new instance of the recipe detail activity is added to the stack. When a user navigates back, the current recipe detail activity is popped off the top of the stack and the user is taken back to the recipe list activity. On tablets, this pattern can be simplified by displaying both the recipe list and recipe details view within a single activity. This allows more information to be presented to users at once and to utilize the available screen space more efficiently.

In this chapter, we will introduce the following:

  • Activities and fragments
  • Practical examples on how activities and fragments make the backbone of any Android app
  • How to create activities and fragments
  • How to create custom activities and fragments
  • How to allow your user to navigate through your application moving between fragments and activities

Tip

If you are more of a practical learner, I would suggest skipping straight to the Creating Activities section.

主站蜘蛛池模板: 江口县| 兖州市| 丰城市| 高淳县| 湛江市| 错那县| 宁武县| 鄂托克旗| 民权县| 宝鸡市| 静宁县| 三河市| 稷山县| 扎赉特旗| 商丘市| 上饶市| 南漳县| 竹山县| 盘锦市| 托里县| 静宁县| 韶山市| 丽江市| 金寨县| 肇东市| 乌什县| 宣汉县| 泰宁县| 海兴县| 崇文区| 边坝县| 兴化市| 金阳县| 宁化县| 嘉义市| 商丘市| 曲麻莱县| 喀什市| 通河县| 桂阳县| 米脂县|