当前位置:   article > 正文

Activity中recreate方法的应用_activity recreate

activity recreate

参考两篇文章:http://blog.csdn.net/watermusicyes/article/details/47392949

    http://blog.csdn.net/droyon/article/details/21275797

recreate可以使用在日间/夜间模式的切换,那么调用recreate()函数将会执行哪些方法呢?

代码:

  1. public class MainActivity extends FragmentActivity implements OnClickListener {
  2. private Button btn;
  3. private int mTheme;
  4. private String THEME = "theme";
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. if (savedInstanceState != null) {
  9. mTheme = savedInstanceState.getInt(THEME);
  10. switchTheme(mTheme);
  11. }
  12. setContentView(R.layout.activity_main);
  13. btn = (Button) findViewById(R.id.btn);
  14. btn.setOnClickListener(this);
  15. Log.e(MainActivity.class.getName(), "onCreate");
  16. }
  17. @Override
  18. protected void onSaveInstanceState(Bundle savedInstanceState) {
  19. super.onSaveInstanceState(savedInstanceState);
  20. Log.e(MainActivity.class.getName(), "onSaveInstanceState");
  21. savedInstanceState.putInt(THEME, mTheme);
  22. }
  23. @Override
  24. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  25. super.onRestoreInstanceState(savedInstanceState);
  26. Log.e(MainActivity.class.getName(), "onRestoreInstanceState");
  27. }
  28. @Override
  29. protected void onStart() {
  30. super.onStart();
  31. Log.e(MainActivity.class.getName(), "onStart");
  32. }
  33. @Override
  34. protected void onResume() {
  35. super.onResume();
  36. Log.e(MainActivity.class.getName(), "onResume");
  37. }
  38. private void switchTheme(int theme) {
  39. switch (mTheme) {
  40. case android.R.style.Theme_Holo_Light:
  41. mTheme = android.R.style.Theme_Black_NoTitleBar;
  42. break;
  43. case android.R.style.Theme_Black_NoTitleBar:
  44. mTheme = android.R.style.Theme_Holo_Light;
  45. break;
  46. default:
  47. mTheme = android.R.style.Theme_Holo_Light;
  48. break;
  49. }
  50. setTheme(mTheme);
  51. }
  52. @SuppressLint("NewApi")
  53. @Override
  54. public void onClick(View v) {
  55. recreate();
  56. }
  57. }


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context="com.example.testandroid.MainActivity" >
  10. <Button
  11. android:id="@+id/btn"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="recreate" />
  15. </RelativeLayout>

点击recreate按钮可以看到打印相关的信息:



可以看到这里调用recreate方法会比正常启动Activity多调用了onSaveInstanceState和onRestoreInstanceState,并且onSaveInstanceState在onCreate方法之前调用。


注意:(1)

  1. if (savedInstanceState != null) {
  2. mTheme = savedInstanceState.getInt(THEME);
  3. switchTheme(mTheme);
  4. }
这部分代码要在setContentView(R.layout.activity_main)代码之前调用,否则改变不了主题。

(2)recreate()方法是在Android3.0引入的,所以如果在3.0之前使用会出现错误,如下图所示:



声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号