近期总结 facebook google+ Twitter sign-in fragment 使用

1. 相关资料

blog: 关于 Google + 以及 Facebook 第三方登录实现的一点总结

g + 官方教程: G+ start

facebook 官方教程: Facebook start

iCCP: Not recognizing known sRGB profile

  • 今天做分享的时候遇到了这个问题:

    [2016-04-01 11:24:04 - Dex Loader] Unable to execute dex: method ID not in [0, 0xffff]: 65536
    [2016-04-01 11:24:04 - VIVAT_SHARESDK] Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536

大项目中遇到的问题看这个博客

  • iCCP: Not recognizing known sRGB profile 删除 png 图片内嵌的 iCCP profile sRGB 报错

今天有碰见一个坑,改其他代码,然后在编译的时候就出现这个问题,对就是这个问题。网上查了资料,也就这个资料最全面,大家可以去看见 http://my.oschina.net/1pei/blog/479162?fromerr=ARrUPlGS

处理这个问题我使用了一种方法,记录下来以便以后使用

步骤 1: 下载 Image Magick http://www.imagemagick.com.cn/download.html. 如果是 windows 的,请下载含 dll 的

步骤 2: 在要处理的文件夹使用如下命令 ,一定要在要处理的文件夹使用

//WINDOWS使用
set fn=E:\Program Files\ImageMagick-6.9.0-Q16\convert.exe
for /f "tokens=*" %i in ('dir/s/b *.png') do "%fn%" "%i" -strip "%i"
(因为是window的,所以把%%i改为%i)
//LINUX使用
set fn=E:\Program Files\ImageMagick-6.9.0-Q16\convert.exe
for /f "tokens=*" %%i in ('dir/s/b *.png') do "%fn%" "%%i" -strip "%%i"

2. 遇到问题

  • 1.“This client application’s callback url has been locked”.

    使用 Twitter signin 时遇到了这个问题,这个错误信息是在 logcat 中找到的,原因是在 Twitter 的 Settings 里勾选了 “Enable Callback Locking (It is recommended to enable callback locking to ensure apps cannot overwrite the callback url)” 选项,这个选项表示不允许 app 本地更改 callback url。也可看这个页面

3.add() vs. replace()

  • 只有在 Fragment 数量大于等于 2 的时候,调用 add() 还是 replace() 的区别才能体现出来。

    当通过 add() 连续两次添加 Fragment 的时候,每个 Fragment 生命周期中的 onAttach()-onResume() 都会被各调用一次,而且两个 Fragment 的 View 会被同时 attach 到 containerView。

    同样,退出 Activty 时,每个 Fragment 生命周期中的 onPause()-onDetach() 也会被各调用一次。

    但当使用 replace() 来添加 Fragment 的时候,第二次添加会导致第一个 Fragment 被销毁,即执行第二个 Fragment 的 onAttach() 方法之前会先执行第一个 Fragment 的 onPause()-onDetach() 方法,同时 containerView 会 detach 第一个 Fragment 的 View。

  • 调用 show() & hide() 方法时.

    Fragment 的生命周期方法并不会被执行,仅仅是 Fragment 的 View 被显示或者​隐藏。而且,尽管 Fragment 的 View 被隐藏,但它在父布局中并未被 detach,仍然是作为 containerView 的 childView 存在着。相比较下,attach() & detach() 做的就更彻底一些。一旦一个 Fragment 被 detach(),它的 onPause()-onDestroyView() 周期都会被执行。

    同时 Fragment 的 View 也会被 detach。在重新调用 attach() 后,onCreateView()-onResume() 周期也会被再次执行。

  • remove()

    其实看完上面的分析,remove() 方法基本也就明白了。相对应 add() 方法执行 onAttach()-onResume() 的生命周期,remove() 就是完成剩下的 onPause()-onDetach() 周期。

4.FragmentTransaction add 和 replace 区别

使用 FragmentTransaction 的时候,它提供了这样两个方法,一个 add , 一个 replace .add 和 replace 影响的只是界面,而控制回退的,是事务。

  • add 是把一个 fragment 添加到一个容器 container 里。

    Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

public abstract FragmentTransaction add (int containerViewId, Fragment fragment, String tag)
  • replace 是先 remove 掉相同 id 的所有 fragment,然后在 add 当前的这个 fragment。

    Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

public abstract FragmentTransaction replace (int containerViewId, Fragment fragment, String tag)

在大部分情况下,这两个的表现基本相同。因为,一般,咱们会使用一个 FrameLayout 来当容器,而每个 Fragment 被 add 或者 replace 到这个 FrameLayout 的时候,都是显示在最上层的。所以你看到的界面都是一样的。但是,使用 add 的情况下,这个 FrameLayout 其实有 2 层,多层肯定要比一层的来得浪费,所以还是推荐使用 replace。当然有时候还是需要使用 add 的。比如要实现轮播图的效果,每个轮播图都是一个独立的 Fragment,而他的容器 FrameLayout 需要 add 多个 Fragment,这样他就可以根据提供的逻辑进行轮播了。

而至于返回键的时候,这个跟事务有关,跟使用 add 还是 replace 没有任何关系。

5. 要想 fragment 完整地执行生命周期

fragment 跳转是要使用 replace() 方法,并一定要指定 tag,否则有些方法不会执行(比如 onResume),例如:

getFragmentManager()
.beginTransaction()
.replace(R.id.base_container,
inputVerifyCodeFragment,"tag_code")
.addToBackStack(null).commit();

6.fragment 事件穿透

如果发现 fragment2 的点击事件可以被 fragment 栈下一层的 fragment1 获取到,可以在 fragment2 布局的根部加上:android:clickable=”true”。问题解决



本文采用知识共享署名 2.5 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自 Agehua’s Blog,并保持转载后文章内容的完整。本人保留所有版权相关权利。

本文链接:http://agehua.github.io/2016/03/15/facebook-google+signin/

Share Comments
Loading comments...