【Android】2015.08.31 安卓官方文档: Day3  第一行代码 Day1 Count:3

-----------------------------------------------
Android Official Documents
Day3
Managing  the activity lifecycle
-----------------------------------------------
Questions:



Points:
1. situation that should override onXxx()?
onCreate()
  • perform basic application startup logic that should happen only once for the entire life of the activity
  • (like: define the user interface and possibly instantiate some class-scope variables.)
  • verify that required system features are enabled.

OnRestoreInstanceState()
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
    
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}



onResume()
  • Initialise components that you release during onPause() 
  • perform any other initializations that must occur each time the activity enters the Resumed state (such as begin animations and initialize components only used while the activity has user focus).

onPause()     
  • Stop animations or other ongoing actions that could consume CPU.
  • Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
  • Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
  • Don’t put heavy-load methods here, like writing data to database, it will increase the time of transit  to next activity.

OnStop()
  •  perform larger, more CPU intensive shut-down operations, such as writing information to a database.
  • released most of your resources
  • The system also keeps track of the current state for each View in the layout, (if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.)

onSaveInstanceState()
public   void  onRestoreInstanceState ( Bundle  savedInstanceState )   {
   
// Always call the superclass so it can restore the view hierarchy
   
super . onRestoreInstanceState ( savedInstanceState );
   
   
// Restore state members from saved instance
    mCurrentScore 
=  savedInstanceState . getInt ( STATE_SCORE );
    mCurrentLevel 
=  savedInstanceState . getInt ( STATE_LEVEL );
}

onDestroy()
  • your activity includes background threads that you created during onCreate()
  • other long-running resources that could potentially leak memory if not properly closed

2.Rotate the screen
 Your activity will be destroyed and recreated each time the user rotates the screen,  because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

3. Bundle
By default, the system uses the  Bundle  instance state to save information about each  View  object in your activity layout (such as the text value entered into an  EditText  object)
As the system begins to stop your activity, it calls  onSaveInstanceState()  (1) so you can specify additional state data you'd like to save in case the  Activity  instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the  onCreate()  method (2) and the onRestoreInstanceState()  method (3)





Terms:
1. resume: 恢复,继续



-----------------------------------------------
第一行代码
Day3
第一章
Hello World
-----------------------------------------------
Questions:
1. Toast.makeText() 如何自己设置显示时长?

2. context 与Activity的区别?

3. finish()后, Toast()放在finish前后都会显示,为什么?难道finish()之后,不是立马停止执行代码么,还是要返回函数后才完?

4. 点击Back键,注销Acitivity?
对,因为点击back键,会调用onBackPressed(), 而该方法里面调用了finish(), 注销掉返回栈中最上面的Activity.
—chu’chu 

5. 隐式Intent的好处?

6. 跳转到另一个Activity后,该Activity的状态是什么?


Points:
1. Dalvik
是专门为移动设备定制的JVM,针对手机内,CPU资源存有限做了优化,它需要把 .java -> bytecode -> .dex 专门压缩格式
但是被指责为拖慢安卓速度的根源,现在有了好的替代平 ,ART: Android Runtime 

2. 去掉ActionBar()
   有两种方法  
     1⃣️requestWindowFeature(Window.FEATURE_NO_TITLE); //进入全屏模式,这里就是不存在ActionBar这个对象了,是remove
     2⃣️getActionBar().hide();//非全屏模式,隐藏ActionBar对象,是hide

如果1⃣️2⃣️一起调用
编译时会报错 :java.lang.RuntimeException:  Unable to start activity ComponentInfo{com.smile.activitytest/com.smile.activitytest.FirstActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference

如果2⃣️1⃣️一起调用:
编译时报错: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smile.activitytest/com.smile.activitytest.FirstActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
* getActionBar().setDisplayShowTitleEnabled(false);可以不显示文字,但是ActionBar还在

3.  this , Activity.this的区别
Intent intent = new Intent(this, SecondActivity.class);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.
Activity.this points to the instance of the Activity you are currently in.

Activity.this 和 Activity.class的区别
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
MainActivity中的this 就是获得当前对象,在你的程序里就是获得MainActivity这个对象
class 指类

4.DEFAULT
< category  android :name= "android.intent.category.DEFAULT /> 
DEFAULT 一定要大写,亲爱的。虽然有它有小写的选择。

5. intent ACTION_VIEW
  • 跳转是以 Activity 为单位,不是Application.
  • 在intent-fiflter中配置好action 为ACTION_VIEW, category 后还不能出现 Open with的选项,还至少要配置下scheme.

6. 一个有趣的bug
在往前传值的过程中,在这里我忘了注释后面一句
startActivityForResult(intent, 1); 
startActivity(intent);
结果是能运行,我发现我点了两次SecondActivity中的button_2,才回到FirstActivity,而且能接受到传回的值。
说明:这里真的开了两个一样的SecondActivity.
经过测试:
startActivityForResult(intent,  1);  //这里开了这个方法 就不要
startActivityForResult(intent,  1);
startActivity(intent);
startActivity(intent);
也是开了四个,而且不会报错。





Terms:


Conclusion:
  • 建立HelloWorld
  • 了解文件目录
  • Log使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值