本文主要是介绍Activity生命周期+四种模式——枯燥重要(五),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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);
// }
//}@Overrideprotected void onStart() { //开始启动super.onStart();Log.e("====MainActivity=","onStart");}@Overrideprotected void onRestart() { //重新启动重新开始super.onRestart();Log.e("====MainActivity=","onRestart");}@Overrideprotected void onResume() { //super.onResume();Log.e("====MainActivity=","onResume");}@Overrideprotected void onPause() { //暂停停止super.onPause();Log.e("====MainActivity=","onPause");}@Overrideprotected void onStop() {super.onStop();Log.e("====MainActivity=","onStop");}@Overrideprotected 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"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/bt1"android:text="跳转画面"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/bt2"android:layout_below="@+id/bt1"android:text="跳转画面返回结果"/><TextViewandroid: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" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/bt3"android:layout_below="@+id/tv"android:text="你高中数学体育老师教?"/><TextViewandroid: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" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btqq"android:layout_below="@+id/tv1"android:text="QQ登陆"/><TextViewandroid: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;@Overrideprotected 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() {@Overridepublic 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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tv"android:text="问题:"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tv1"android:layout_below="@id/tv"android:text="问题:::"android:textSize="20dp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/et"android:text="请输入问题答案"android:layout_below="@id/tv1"/><Buttonandroid: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 {@Overrideprotected 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();
}}@Overrideprotected void onStart() { //开始启动super.onStart();Log.e("====SecondActivity=","onStart");Log.d("====SecondActivity=","onStart");Log.w("====SecondActivity=","onStart");Log.v("====SecondActivity=","onStart");Log.i("====SecondActivity=","onStart");}@Overrideprotected void onRestart() { //重新启动重新开始super.onRestart();Log.e("====SecondActivity=","onRestart");}@Overrideprotected void onResume() { // 控件绘制完成super.onResume();Log.e("====SecondActivity=","onResume");}@Overrideprotected void onPause() { //暂停停止super.onPause();Log.e("====SecondActivity=","onPause");}@Overrideprotected void onStop() { //停止super.onStop();Log.e("====SecondActivity=","onStop");}@Overrideprotected void onDestroy() { //破坏毁坏销毁super.onDestroy();Log.e("====SecondActivity=","onDestroy");</span><span style="font-size:18px;">}
}
</span>
4.QqA
Activity程序代码
<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;@Overrideprotected 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() {@Overridepublic 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生命周期+四种模式——枯燥重要(五)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!