android_41_activity生命周期
来源:互联网 发布:php 手机短信接口 编辑:程序博客网 时间:2024/06/05 08:40
效果:
一开始运行mainActivity输出结果如下:
点击 跳转到第2个activity输出如下:
点击返回到mainActivity输出如下:
再次点击,退出应用,输出如下:
注意:此时mainActivity仍驻留内存,成为空进程,并未直正消失(因为如果立马重新启动,输出的静态整型变量的数值是接着上一次的值继续再增加)
清单文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sg31.activitylifecycle" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.sg31.activitylifecycle.SecondActivity" ></activity> </application></manifest>
mainActivity布局文件:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sg31.activitylifecycle.MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转至第二个" android:onClick="jumpBtnClicked" /> </LinearLayout>
第2个Activity布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个Activity" /> </LinearLayout>
mainActivity代码:
package com.sg31.activitylifecycle;import android.support.v7.app.ActionBarActivity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends ActionBarActivity {static int i = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("sg__mainActivity__create"+"__"+i++);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overrideprotected void onStart() {super.onStart();System.out.println("sg__mainActivity__start"+"__"+i++);}@Overrideprotected void onResume() {super.onResume();System.out.println("sg__mainActivity__resume"+"__"+i++);}@Overrideprotected void onPause() {super.onPause();System.out.println("sg__mainActivity__pause"+"__"+i++);}@Overrideprotected void onStop() {super.onStop();System.out.println("sg__mainActivity__stop"+"__"+i++);}@Overrideprotected void onDestroy() {super.onDestroy();System.out.println("sg__mainActivity__destroy"+"__"+i++);}@Overrideprotected void onRestart() {super.onRestart();System.out.println("sg__mainActivity__restart"+"__"+i++);}public void jumpBtnClicked(View v) {Intent intent = new Intent(this, SecondActivity.class);startActivity(intent);}}
第2个Activity代码:
package com.sg31.activitylifecycle;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;public class SecondActivity extends ActionBarActivity {static int i = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);System.out.println("sg__secondActivity__create"+"______"+i++);}@Overrideprotected void onStart() {super.onStart();System.out.println("sg__secondActivity__start"+"______"+i++);}@Overrideprotected void onResume() {super.onResume();System.out.println("sg__secondActivity__resume"+"______"+i++);}@Overrideprotected void onPause() {super.onPause();System.out.println("sg__secondActivity__pause"+"______"+i++);}@Overrideprotected void onStop() {super.onStop();System.out.println("sg__secondActivity__stop"+"______"+i++);}@Overrideprotected void onDestroy() {super.onDestroy();System.out.println("sg__secondActivity__destroy"+"______"+i++);}@Overrideprotected void onRestart() {super.onRestart();System.out.println("sg__secondActivity__restart"+"______"+i++);}}
0 0
- android_41_activity生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- 生命周期
- Python基础——特殊变量名
- IP地址及子网掩码
- 【Operator】【Comcast】有线电视运营商部署LoRa进军物联网
- ActiveRecord模式整理
- debug代码
- android_41_activity生命周期
- FL Studio中如何设置教程
- 用http和https请求进行访问数据
- Win7下U盘安装CentOS-7-x86_64-DVD-1503-01(入门版)
- RealTimeRendering第三版的翻译
- Java程序挂掉的几种可能
- Java ME、Java SE、Java EE区别
- SQL GETDATE()日期格式化函数
- logback冲突导致日志级别失效的解决办法