Activity生命周期+四种模式——枯燥重要(五)
来源:互联网 发布:陕西大数据集团官网 编辑:程序博客网 时间:2024/06/07 10:54
Activity生命周期+四种模式
了解android系统的四大组件
掌握Activity的生命周期
LogCat的使用
Android系统有四个重要的组件,分别是
Activity
Service
BroadcastReceiver
ContentProvider
Activity是Android程序的呈现层,显示可视化的用户
界面,并接收与用户交互所产生的界面事件
Android应用程序可以包含一个或多个Activity,一般在
程序启动后会呈现一个Activity,用于提示用户程序已经
正常启动
了解Androi系统四个重大组件 Activity SERvice后台BroadcastReceive广播ContentProvider内容提供者数据共享
Activity生命周期
系统调用方面——事件回调函数
打开onCreate(创建)onStart(开始)onResume(控件绘制完成,跟用户交互)先运行重生:
onRestart onStart onResume(onRestart代替onCreate)退出onPause(暂停)onStop(停止)
onDestroy(销毁)跳转页面,先是自己onPause(暂停)跳转页面的onCreate/onRestart()然后是自己的onStop(停止)
重点重点重点重要的事说三遍
掌握
四种模式。
android:launchMode="singleInstance"
单任务模式android:launchMode="singleTask" 首页想退出程序。在它之前都销毁了。
Singletop 加载模式//在栈顶服用不再重建
单实例模式android:launchMode="singleInstance"
1.standard 标准模式也是Activity默认的加载模式。按调用顺序压入Activity栈
2.singleTop 栈顶单一模式,当Actiwity栈顶已存在实例,则不重新创建,佛祖创建。
3.singleTask 单任务模式,当Actiwity栈中存在实例,将销毁栈中此实例之上的实例。
4.singleInstance单实例模式,实例创建后放在单独的栈中,不与其他实例共享Activity栈
1.MainActivity程序代码
package com.example.jreduch729;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity {private Button bt1; private Button bt2,bt3; private Button btqq; private TextView tv; private TextView tv1,tvqq; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("====MainActivity=","onCreate"); tv1=(TextView)findViewById(R.id.tv1) ; Intent intent4=getIntent(); String d=intent4.getStringExtra("daan"); tv1.setText("问题结果是:"+d); tvqq=(TextView)findViewById(R.id.tvqq) ; Intent intent2=getIntent(); String use=intent2.getStringExtra("use"); Intent intent3=getIntent(); String usepsw=intent3.getStringExtra("usepsw"); tvqq.setText("QQ账号+密码:"+use+"+"+usepsw); bt1=(Button)findViewById(R.id.bt1); bt2=(Button)findViewById(R.id.bt2); bt3=(Button)findViewById(R.id.bt3); tv=(TextView)findViewById(R.id.tv) ; bt1.setOnClickListener(new View.OnClickListener() { @Override //跳转 public void onClick(View v) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("arg1",5); intent.putExtra("arg2",6); intent.putExtra("arg3","hh"); st<span style="font-size:14px;">artActivityForResult(intent,110); //一对 } }); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MainTeacherActivity.class); ; intent.putExtra("arg0","你高中数学体育老师教?"); startActivity(intent); } }); btqq=(Button)findViewById(R.id.btqq); btqq.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,QqActivity.class); startActivity(intent); } }); } //返回结果都要走这个程序 @Override //一对 protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.e("==requestCode",""+requestCode); Log.e("==resultCode",""+resultCode); tv.setText("结果是:"+resultCode+data.getStringExtra("result2")+data.getStringExtra("result1"));// if(requestCode==100){//// }else if(requestCode==110){// tv.setText("结果是:"+resultCode);// }// } @Override protected void onStart() { //开始启动 super.onStart(); Log.e("====MainActivity=","onStart"); } @Override protected void onRestart() { //重新启动重新开始 super.onRestart(); Log.e("====MainActivity=","onRestart"); } @Override protected void onResume() { // super.onResume(); Log.e("====MainActivity=","onResume"); } @Override protected void onPause() { //暂停停止 super.onPause(); Log.e("====MainActivity=","onPause"); } @Override protected void onStop() { super.onStop(); Log.e("====MainActivity=","onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.e("====MainActivity=","onDestroy"); }}</span>
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.example.jreduch729.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt1" android:text="跳转画面" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt2" android:layout_below="@+id/bt1" android:text="跳转画面返回结果" /> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/tv" android:layout_below="@+id/bt2" android:textSize="20dp" android:text="结果是::" android:textColor="#f81111" android:textStyle="normal|bold|italic" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt3" android:layout_below="@+id/tv" android:text="你高中数学体育老师教?" /> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/tv1" android:layout_below="@+id/bt3" android:textSize="20dp" android:text="问题结果" android:textColor="#f81111" android:textStyle="normal|bold|italic" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btqq" android:layout_below="@+id/tv1" android:text="QQ登陆" /> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/tvqq" android:layout_below="@+id/btqq" android:textSize="20dp" android:text="QQ账号+密码" android:textColor="#f81111" android:textStyle="normal|bold|italic" /></RelativeLayout></span>
2.MainTeacherActivity程序代码
<span style="font-size:14px;">package com.example.jreduch729;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainTeacherActivity extends AppCompatActivity {public Button bt1; public TextView tv1; public TextView et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_teacher); tv1=(TextView)findViewById(R.id.tv1) ; Intent intent5=getIntent(); String d1=intent5.getStringExtra("arg0"); tv1.setText("问题——"+d1); bt1=(Button)findViewById(R.id.bt1); final EditText daan=(EditText)findViewById(R.id.et); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainTeacherActivity.this,MainActivity.class); intent.putExtra("daan", daan.getText().toString()); startActivity(intent); } }); }}</span>
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.example.jreduch729.MainTeacherActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:text="问题:"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:layout_below="@id/tv" android:text="问题:::" android:textSize="20dp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et" android:text="请输入问题答案" android:layout_below="@id/tv1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt1" android:layout_below="@id/et" android:text="完成" android:layout_alignParentEnd="true" /></RelativeLayout></span>3.SecondActivity程序代码
<span style="font-size:18px;">package com.example.jreduch729;</span><span style="font-size:14px;">import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Log.e("====SecondActivity=","onCreate"); Intent intent=getIntent(); int arg1=intent.getIntExtra("arg1",0); int arg2=intent.getIntExtra("arg2",0); String arg3=intent.getStringExtra("arg3"); intent.putExtra("result1",arg3); intent.putExtra("result2","数据返回"+arg3);if (arg1>0&&arg2>0) { // setResult(120); setResult(arg1 + arg2,intent); finish();} } @Override protected void onStart() { //开始启动 super.onStart(); Log.e("====SecondActivity=","onStart"); Log.d("====SecondActivity=","onStart"); Log.w("====SecondActivity=","onStart"); Log.v("====SecondActivity=","onStart"); Log.i("====SecondActivity=","onStart"); } @Override protected void onRestart() { //重新启动重新开始 super.onRestart(); Log.e("====SecondActivity=","onRestart"); } @Override protected void onResume() { // 控件绘制完成 super.onResume(); Log.e("====SecondActivity=","onResume"); } @Override protected void onPause() { //暂停停止 super.onPause(); Log.e("====SecondActivity=","onPause"); } @Override protected void onStop() { //停止 super.onStop(); Log.e("====SecondActivity=","onStop"); } @Override protected void onDestroy() { //破坏毁坏销毁 super.onDestroy(); Log.e("====SecondActivity=","onDestroy");</span><span style="font-size:18px;"> }}</span>4.QqAActivity程序代码
<span style="font-size:14px;">package com.example.jreduch729;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class QqActivity extends AppCompatActivity { public Button bt1; public TextView et1; public TextView et2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qq); final EditText use=(EditText)findViewById(R.id.et1); final EditText usepsw=(EditText)findViewById(R.id.et2); bt1=(Button)findViewById(R.id.bt1); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(QqActivity.this,MainActivity.class); intent.putExtra("use", use.getText().toString()); intent.putExtra("usepsw", usepsw.getText().toString()); startActivity(intent); } }); }}</span>
作者:冲天之峰 20160729
- Activity生命周期+四种模式——枯燥重要(五)
- Activity生命周期和四种启动模式
- Activity的生命周期和Activity的四种启动模式
- android基础重点总结(分层架构,五中布局,activity的生命周期和四种启动模式)
- android activity的生命周期,四种启动模式
- Activity的生命周期和四种开启模式之我见
- Android的Activity生命周期以及四种启动模式
- Activity的生命周期及其四种启动模式
- 面试被鄙视之路——activity的生命周期及启动的四种模式,栈管理
- Android启动过程 && Activity生命周期 && Activity开机自启动 && Activity的四种启动模式
- Activity的生命周期(五)——Activity生命周期的应用场景
- Activity的生命周期(四)——多个Activity相互交互的生命周期
- Android五大布局,四大组件,五种存储方式,activity四种启动模式
- Activity的四种状态和生命周期
- Activity的生命周期以及四种形态
- Android 基础学习——Activity四种启动模式
- Android面试——Activity的四种启动模式
- Android——Activity四种启动模式
- php发送email最终版 (案例)
- FragmentPagerAdapter 的那些坑
- hdoj2612Find a way(两次bfs)
- 论assert(0)的作用
- ACM中常见错误对应表
- Activity生命周期+四种模式——枯燥重要(五)
- POJ 3126 Prime Path(首相的无厘头要求,这个是不是华盛顿,BFS)
- ubuntu14.04安装wine
- 从数组中获取最值
- CreateProcess创建新的进程
- Opencv【7】---IplImage中四字节对其问题
- POJ 2891 Strange Way to Express Integers(CRT非互质)
- Studio中xml布局的design不显示“ Rendering Problems Exception raised during rendering: com/android/ut”,解决方案!
- 生活、工作点滴(二)