2013.8.26 横竖屏旋转,Bundle,Fragment实例

1,学习新知识时,要学会联想,想想过去是怎么搞的,新旧方式之间有什么不同,优劣在哪。

2,Activity 生命周期方法复习:

onCreate,onStart,onResume,Running,onPause,onStop,onDestroy

在Activity执行onStop后但并未执行onDestroy,用户再次返回该Activity时,执行onRestart,onStart, onResume

当Activity横竖屏切换时,Activity会执行onPause,onStop,onDestroy,然后onCreate,onStart,onResume


3,原来:

Inent intent;

intent.putExtra() 的值都存放在Intent中的一个Bundle里,

intent.getExtras()时,将这个Bundle返回。

    /**
     * Retrieves a map of extended data from the intent.
     *
     * @return the map of all extras previously added with putExtra(),
     * or null if none have been added.
     */
    public Bundle getExtras() {
        return (mExtras != null)
                ? new Bundle(mExtras)
                : null;
    }
    public Intent putExtra(String name, boolean value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putBoolean(name, value);
        return this;
    }

4,终于把Android官方文档上关于Fragment的例子自己写了一遍。其中最重要的是要保存旋转屏幕前的状态,待下一次重新生成界面时,把原来的状态取出来。还有要判断当前屏幕方向。将横屏布局放在layout-land中,纵屏布局默认在layout中。使用Fragment,使Activity更简洁了,只处理屏幕适配。业务逻辑则交给了Fragment,提高了Fragment的重用性。

下面是图:

2013.8.26 横竖屏旋转,Bundle,Fragment实例_第1张图片2013.8.26 横竖屏旋转,Bundle,Fragment实例_第2张图片2013.8.26 横竖屏旋转,Bundle,Fragment实例_第3张图片2013.8.26 横竖屏旋转,Bundle,Fragment实例_第4张图片

这里是源码,其中参考了 Neil Goodman 的例子,在这里,致谢!

源码下载

你可能感兴趣的:(2013.8.26 横竖屏旋转,Bundle,Fragment实例)