码迷,mamicode.com
首页 > 其他好文 > 详细

(八)activity的生命周期

时间:2014-10-14 00:59:17      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   color   io   os   ar   

8.1 activity的7个生命周期方法

onCreate()、onStart()、onResume()、onpause()、onstop()、ondestroy()、onRestart()方法。onRestart()方法是当一个已经创建的Activity重新位于前台时,会调用此方法,其他的六个生命周期函数调用时机如下所示:

bubuko.com,布布扣

8.2 activity的生命周期

bubuko.com,布布扣

8.3  生命周期函数的应用场景

ondestroy():程序被销毁的时候调用,比较适合数据的持久化操作,保存数据。

onStart():UI界面的更新或业务逻辑的判断

8.4 第一个例子程序  ActivityLifeCycle程序

8.4.1 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical"
    tools:context="com.example.activitylifecycle.MainActivity" >
    <Button 
        android:onClick="click1"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击进入第二个Activity"/>
      <Button 
        android:onClick="click2"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击进入对话框风格的Activity"/>

   <!--  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" /> -->

</LinearLayout>

 

8.4.2 MainActivity.java程序

package com.example.activitylifecycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("onCreate()");
    }

    public void click1(View view){
        Intent intent=new Intent(this,SecondActivity.class);
        startActivity(intent);
        
    }
    public void click2(View view){
        Intent intent=new Intent(this,ThirdActivity.class);
        startActivity(intent);
        
    }
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        System.out.println("onStart()");
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        System.out.println("onRestart()");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        System.out.println("onResume()");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("onPause()");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        System.out.println("onStop()");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("onDestroy()");
    }

}

8.4.3 SecondActivity.java和 ThirdActivity.java里面的内容都是空的

8.4.4 在AndroidManifest.xml中添加

  <activity
            android:name=".SecondActivity"
            android:label="@string/SecondActivityapp_name" >
        </activity>
        <activity
            android:theme="@android:style/Theme.Dialog"
            android:name=".ThirdActivity"
            android:label="@string/ThirdActivityapp_name" >
        </activity>

 

 

8.5 第二个例子程序   网络连接的判断

8.5.1 MainActivity.java中的代码

 

package com.example.networktest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Contacts;
import android.text.AlteredCharSequence;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        ConnectivityManager cm = (ConnectivityManager) this
                .getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo netWorkInfo = cm.getActiveNetworkInfo();
        if (netWorkInfo != null && netWorkInfo.isAvailable()) {
            Toast.makeText(MainActivity.this, "网络可用", 1).show();
        } else {
            AlertDialog.Builder builder = new Builder(this);
            builder.setTitle("网络不可用提示对话框");
            builder.setMessage("当前系统网络不可用,请点击确定按钮设这");
            builder.setPositiveButton("确定", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "网络不可用", 1).show();
                    /*
                     * Intent intent=new Intent();
                     * intent.setClassName("com.android.settings",
                     * "com.android.settings.WirelessSettings");
                     * startActivity(intent);
                     */
                }
            });
            builder.create().show();

        }
    }

}

 

 

 8.5.2 在AndroidManifest.xml中添加访问网络状态的权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

(八)activity的生命周期

标签:des   android   style   blog   http   color   io   os   ar   

原文地址:http://www.cnblogs.com/fuyanan/p/4023363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!