关于activity的活动周期以及intent

1.  关以activity 的活动周期

    

  1 package com.bwf.a6_actlife;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.content.res.Configuration;
  6 import android.util.Log;
  7 import android.view.Menu;
  8 import android.widget.Toast;
  9 /**
 10  * Activity的生命周期方法的调用规则
 11  *         第一次启动:onCreate-->onStart-->onResume
 12  *         按HOME键:onPuase-->onStop
 13  *         点击图标启动:onRestart-->onStart-->onResume
 14  *         按BACK键:onPause-->onStop-->onDestroy此时Activity对象已经被销毁,生命周期就结束
 15  *         转屏时:onPause-->onStop-->onDestroy销毁上一个Activity对象,
 16  *              onCreate-->onStart-->onResume重新创建了一个Activity对象
 17  *         onSaveInstanceState在Activity进行异常注销的时候调用比如转屏、切换语言、内存吃紧时,用来保存数据
 18  *             调用时机是onPause-->onSaveInstanceState-->onStop
 19  *         onRestoreInstanceState也是在上一次异常注销后需要恢复时调用,用来恢复数据
 20  *             调用时机onStrat-->onRestoreInstanceState-->onResume
 21  * */
 22 public class MainActivity extends Activity {
 23     final String TAG = "fanhy";
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_main);
 28         
 29         myLog("... onCreate ...");
 30         if(savedInstanceState != null){
 31             myLog("。。。"+savedInstanceState.getString("my_data"));
 32         } else{
 33             myLog("。。。null");
 34         }
 35     }
 36     
 37     @Override
 38     protected void onRestoreInstanceState(Bundle savedInstanceState) {
 39         // TODO Auto-generated method stub
 40         super.onRestoreInstanceState(savedInstanceState);
 41         
 42         if(savedInstanceState != null){
 43             myLog("onRestoreInstanceState..."+savedInstanceState.getString("my_data"));
 44         }else{
 45             myLog("onRestoreInstanceState...没有要恢复的数据");
 46         }
 47     }
 48 
 49     /**
 50      * 如果在AndroidManifest.xml中设置了android:configChanges属性则会调用onSaveInstanceState
 51      * 但不会调用onRestoreInstanceState
 52      * */
 53     @Override
 54     protected void onSaveInstanceState(Bundle outState) {
 55         // TODO Auto-generated method stub
 56         super.onSaveInstanceState(outState);
 57         myLog(".... onSaveInstanceState ...");
 58         outState.putString("my_data", "保存一下");
 59     }
 60     
 61     @Override
 62     public void onConfigurationChanged(Configuration newConfig) {
 63         // TODO Auto-generated method stub
 64         super.onConfigurationChanged(newConfig);
 65         
 66         myLog("...onConfigurationChanged,要更新的操作就放到此方法中实现...方向:"
 67                 +newConfig.orientation+",语言:"+newConfig.locale);
 68         
 69         Toast.makeText(getApplicationContext(), "执行onConfigurationChanged", 
 70                 Toast.LENGTH_LONG).show();
 71     }
 72     
 73     @Override
 74     protected void onStart() {
 75         // TODO Auto-generated method stub
 76         super.onStart();
 77         myLog("... onStart ...");
 78     }
 79 
 80     @Override
 81     protected void onRestart() {
 82         // TODO Auto-generated method stub
 83         super.onRestart();
 84         myLog("... onRestart ...");
 85     }
 86 
 87     @Override
 88     protected void onResume() {
 89         // TODO Auto-generated method stub
 90         super.onResume();
 91         myLog("... onResume ...");
 92     }
 93 
 94     @Override
 95     protected void onPause() {
 96         // TODO Auto-generated method stub
 97         super.onPause();
 98         myLog("... onPause ...");
 99     }
100 
101     @Override
102     protected void onStop() {
103         // TODO Auto-generated method stub
104         super.onStop();
105         myLog("... onStop ...");
106     }
107 
108     @Override
109     protected void onDestroy() {
110         // TODO Auto-generated method stub
111         super.onDestroy();
112         myLog("... onDestroy ...");
113     }
114 
115     public void myLog(String s){
116         Log.d(TAG, s);
117     }
118 }

2. 关于intent意图

2.1 主页Java代码

 1 package com.bwf.a6_switchact;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.EditText;
 9 
10 public class MainActivity extends Activity {
11     EditText edt_1,edt_2;
12     
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         
18         edt_1 = (EditText) findViewById(R.id.edt_num_1);
19         edt_2 = (EditText) findViewById(R.id.edt_num_2);
20         
21     }
22     
23     public void mainClick(View v){
24         switch (v.getId()) {
25         case R.id.btn_switch:
26             // 跳转的操作在此分支实现
27             // 创建Intent对象
28             Intent intent = new Intent();
29             // 指定跳转的两个页面
30             // 参数1 当前页面
31             // 参数2 目标页面
32             intent.setClass(MainActivity.this, SecondActivity.class);
33             
34             // 获取两个EditText中的值
35             String str1 = edt_1.getText()+"";
36             String str2 = edt_2.getText()+"";
37             
38             // 将数据直接放入Intent对象
39             //intent.putExtra("num1", str1);
40             //intent.putExtra("num2", str2);
41             
42             // 将数据封装到Bundle对象中
43             Bundle bundle = new Bundle();
44             // 给bundle对象绑定一些值
45             bundle.putString("num1", str1);
46             bundle.putString("num2", str2);
47             // 把bundle绑定到Intent对象
48             intent.putExtras(bundle);
49             
50             // 跳转的方法startActivity
51             startActivity(intent);
52             
53             // 上述步骤合在一起
54             //startActivity(new Intent(MainActivity.this, SecondActivity.class));
55             //finish();
56             break;
57 
58         default:
59             break;
60         }
61     }
62 }

2.2 分页Java代码

 1 package com.bwf.a6_switchact;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.TextView;
 9 
10 public class SecondActivity extends Activity {
11     TextView tv_result;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_second);
16         
17         tv_result = (TextView) findViewById(R.id.tv_result);    
18     }
19 
20     public void secondClick(View v){
21         switch (v.getId()) {
22         case R.id.btn_caculate:
23             // 获取到MainActivity传递过来的Intent对象
24             Intent intent = getIntent();
25             // 从该Intent对象中取出数据
26             //int num1 = Integer.parseInt(intent.getStringExtra("num1"));
27             //int num2 = Integer.parseInt(intent.getStringExtra("num2"));
28             
29             // 通过Intent对象把bundle取出
30             Bundle b = intent.getExtras();
31             int num1 = Integer.parseInt(b.getString("num1"));
32             int num2 = Integer.parseInt(b.getString("num2"));
33             
34             // 加法运算
35             int num = num1 + num2;
36             // 把结果转成需要的字符串
37             String str = num1 + "+" + num2 + "=" + num;
38             // 把该字符串设置到TextView上
39             tv_result.setText(str);
40             
41             break;
42 
43         default:
44             break;
45         }
46     }
47 }

 

posted on 2015-09-14 17:01  敬的男人  阅读(273)  评论(0编辑  收藏  举报