Sean's Note: 7月 2015

2015年7月30日 星期四

Some notes for Fragment's usage

Subclass Fragment

繼承 Fragment 時,要提供 Public empty constructor,否則會出現下列錯誤訊息:

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment 省略: make sure class name exists, is public, and has an empty constructor that is public ...

詳見: http://blog.csdn.net/xplee0576/article/details/43057633

還有 subclass of fragment 如果是 non-static inner class,既使提供了 Public empty
constructor,仍會出現錯誤而且還會造成 Memory Leak(Implicit reference to Activity),
必須是 static class 或 non-inner class。

Fragments Communication

Fragments 的溝通可以分成下列三種情況:
  • Activity to Fragment
    1. Activity 建立 bundle 物件 並透過 Fragment.setArguments(bundle) 來傳遞;而fragment 則透過 Bundle bundle = getArguments() 來接收。
    2. 在 Fragment 中建立 public method 來讓 Activity 引用。
      EX: getSupportFragmentManager().findFragmentById(R.id.fragment).publicMethod(args)
  • Fragment to Activity
    1. 利用 interface(listener) 的方法,讓 Activity 實作 listener。
    2. 在 Activity 建立 public method 讓 fragments 引用。
  • Fragment to Fragment 
    1. 利用上述兩種方式來實現。

How to draw radius background to Button?

在 drawable 資料夾裡,新增一個 xml,定義 shapre 物件和 radius 屬性,範例如下:

// bg_selector_radius_button.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
     <selector>
      <item android:state_pressed="true">
        <shape 
          android:shape="rectangle">
          <corners android:radius="2dp" />
          <solid android:color="#ffff00" />
        </shape>
      </item>
      <item  android:state_enabled="false">
        <shape 
          android:shape="rectangle">
          <corners android:radius="2dp" />
          <solid android:color="#999999" />
        </shape>
      </item>
      <item>
        <shape 
          android:shape="rectangle">
          <corners android:radius="2dp" />
          <solid android:color="#ff0000" />
        </shape>
      </item>
     </selector>
    </item>
</layer-list>

2015年7月22日 星期三

[Android Studio] Code Completion Case Sensitive Setting

Android Studio 的 Code Completion 預設是 Case sensitive 的,
更改設定的方法:
Settings(or Preferences in mac) -> Editor -> Code Completion -> Case sensitive completion -> None

[Android Studio 1.2.2] Debugger stuck

最近開始改用 Android Studio,卻發現 debug 的時候常常卡住,
但 Android Studio 本身並沒有 hang, 後來發現網路上有很多人 report Google 這支 issue,
其中 issue 172523 裡有 Google 人員提到暫時的 workaround 方法:

"For anyone else who might encounter this issue, here is a summary:

The issue shows up in one of two ways: Studio will be responsive, but the debugger will be stuck at either "Collecting Data.." or "Waiting for last debugger command to complete..". This happens on both Dalivk and ART, so all versions of the platform are affected. The issue is more prevalent with Studio 1.2, but exists on all versions of Studio.

The correct fix for this issue is in the platform. The next version of M preview is likely to have this fix (in progress CL here: https://android-review.googlesource.com/#/c/152715/)

Until then we have some workarounds which reduce the probability of hitting this issue. So if you encounter this issue, you can try one of the following:

1. Change your breakpoint to only suspend the thread where it is hit rather than all threads. See comment #82 for more info on how to do this. The next release of Studio 1.2 and Studio 1.3 will be make this the default. (https://android-review.googlesource.com/#/c/152715/)

2. You can turn off various settings in the debugger that invoke methods: These include:
  a) inline debugging (https://www.jetbrains.com/idea/help/inline-debugging.html)
  b) "Enable 'toString()' object view" (Settings | Debugger | Data Views | Java)
  c) "Enable alternative view for Collections classes" (Settings | Debugger | Data Views | Java)

The 2nd option is more severe (it limits the amount of automation the debugger does for you), so we are not enabling that by default. However, if you still see the issue after changing the suspend policy to thread only, then unfortunately, you'll have to do the steps in 2 as well.

Finally, if you still see the issue after both, then that would be a new bug. Please file a new bug with a test case.

Thanks everyone for your patience and your help in providing us with repro cases and stack traces."


結論是 2.a 的 workaround 似乎可行。