Back to Android

Start to read Head First Android Development.

Issues about the xml attribute android:ems

Chapter 3


I get a little confused about what is meant by ems. And found the answer below.

android:ems or setEms(n) sets the width of a TextView to fit a text of n ‘M’ letters regardless of the actual text extension and text size. See wikipedia Em unit

but only when the layout_width is set to "wrap_content". Other layout_width values override the ems width setting.

Adding an android:textSize attribute determines the physical width of the view to the textSize * length of a text of n ‘M’s set above.

Issues about the frament onAttach() method.

When tested the code showed in Chapter 7,

public void onAttach(Activity activity) {
        Log.d("aaaabbb", "aaaaaa");
        this.listListener = (WorkoutListListener)activity;

Android Studio indicated that this method is deprecated, and suggest to use the code

onAttach(Context context)


However, after I changed the code

public void onAttach(Activity activity) {
this.listener = (WorkoutListListener)activity;


    public void onAttach(Context context) {
        Log.d("=======", "onAttach");
        this.listListener = (WorkoutListListener) context;

it occurred to me that the onAttach(Context context) didn’t even be called when running the app on virtual device with an API level of 21. Seem that many people also meet the problem.(eg, see here, and here) And when testing the same code on devices running an API level of 23, it performs perfectly. TL; DR: when the API level is less than 23, override onAttach(Activity). When the API level is equal or bigger than 23, then override onAttach(Context).

Marks on Fragment Life Circle


The onCreate() method in a Fragment is called after the Activity‘s onAttachFragment() but before that Fragment‘s onCreateView().
In this method, you can assign variables, get Intent extras, and anything else that doesn’t involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity‘s onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash.


After the onCreate() is called (in the Fragment), the Fragment‘s onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View to this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null.


As the name states, this is called after the Activity‘s onCreate() has completed. It is called afteronCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).

To sum up…
… they are all called in the Fragment but are called at different times.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView(). Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.

reference from

Found a bug in Android Studio.

In Chapter 9, the book tells us how to add action bars and add action items to the bar. Since I use an API level of 23 to compile the project, I removed the dependency on appCompact-v7, and modified the showAsAction attribute of the item nodes in menu resource file. As is said, if the project has no dependency on v7 appcompact library, showAsAction must be prefixed with


, not


. And can omit the attribute


So the final version of my menu_main.xml becomes:

<menu xmlns:android=""

        android:showAsAction="ifRoom" />

        android:showAsAction="never" />

And then Android Studio code assist showed en error on android:showAsAction

Should use app:showAsAction with the appcompat library with xmlns:app=""less… (Ctrl+F1)

When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace.

Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.

It really disappointed me at first, and I searched on the Internet for several hours to find out how could I make this a correct one. Finally I decided to try compile the module regardless of the error. Surprisingly, no error occurs during the build, and when I install the app and run on the android device, it performs perfect. This morning, I searched the Internet again in order to find some suggestion. And I found people here who have the same problem as me.

This really cost me a lot of time. I should learn from the lesson not to always believe the IDE. The thing that really matters is the final result, believe what you’ve learn and always try the ideas.

Another android studio bug

There’s already an issue.

Cannot see files inside /data via DDMS

It seems that /data has a more strict privilege than the common directories. The solution is to use an Android application called Root Explorer to set the /data directory’s privilege to 777.

What do three dots in parameters mean

When studying the Chapter 12, there’s such a code block:

protected Boolean doInBackground(Integer... drinks) {
    // some codes

The three dots means arbitrary arguments of the specific type.


请原谅我真的不知道这个标题用英文应该怎么说。看到 13 章 Log 部分有个 Log.wft(),书中有段旁注:

There’s also a method you can use to report exceptions that should never happen. According to the Android documentation, wtf means “What a terrible Failure”. We know it means “Welcome to Fiskidagurinn”, which refers to  the Great Fish Day festival held annually in Dalvik, Iceland. Android Developers can often be heard to say “My AVD just took 8 minutes to boot up. WTF??” as
a tribute to the small town that gave its name to the standard Android executable bytecode format.

想到之前还被书中特意放的一段错误代码耍了好久(Fragment 中的 onClick 绑定),我已经决定有机会要买个 Head First 大全集来拜读了,这种文风实在是 1024 个赞。

《Back to Android》有1个想法


电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>