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 onResumeonRestart代替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.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;
    @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









'[IT18掌www.it18zhang.com]001.Hadoop基础篇.pptx' '[IT18掌www.it18zhang.com]014.Hadoop Win7开启网络访问.pptx' '[IT18掌www.it18zhang.com]Kafka.pptx' '[IT18掌www.it18zhang.com]002.VMware下载与安装.pptx' '[IT18掌www.it18zhang.com]015.Hadoop 架构分析.pptx' '[IT18掌www.it18zhang.com]KVM.pptx' '[IT18掌www.it18zhang.com]003.Ubuntu下载与虚拟机下安装.pptx' '[IT18掌www.it18zhang.com]016.Hadoop 架构分析之启动脚本分析.pptx' '[IT18掌www.it18zhang.com]Scala.pptx' '[IT18掌www.it18zhang.com]004.Ubuntu常用命令.pptx' '[IT18掌www.it18zhang.com]017.Hadoop 架构分析之启动脚本总结.pptx' '[IT18掌www.it18zhang.com]Spark Graph编程指南.pptx' '[IT18掌www.it18zhang.com]005.Ubuntu目录与权限.pptx' '[IT18掌www.it18zhang.com]018.Hadoop MapReduce初识.pptx' '[IT18掌www.it18zhang.com]Spark SQL DataFrame Dataset编程指南.pptx' '[IT18掌www.it18zhang.com]006.Ubuntu软件包桌面与增强工具.pptx' '[IT18掌www.it18zhang.com]019.Hadoop MapReduce原理.pptx' '[IT18掌www.it18zhang.com]Spark Streaming编程指南.pptx' '[IT18掌www.it18zhang.com]007.Ubuntu本地软件源与iso制作.pptx' '[IT18掌www.it18zhang.com]019.Hadoop YARN事件分发原理.pptx' '[IT18掌www.it18zhang.com]Spark编程指南.pptx' '[IT18掌www.it18zhang.com]008.Ubuntu虚拟机克隆与Mac地址生成与网络连接方式.pptx' '[IT18掌www.it18zhang.com]020.Hadoop HDFS.pptx' '[IT18掌www.it18zhang.com]Spark编译运行处理.pptx' '[IT18掌www.it18zhang.com]009.Hadoop-Ubuntu下JDK与Hadoop安装配置.pptx' '[IT18掌www.it18zhang.com]021.Hadoop HDFS CLI.pptx' '[IT18掌www.it18zhang.com]Spark基础.pptx' '[IT18掌www.it18zhang.com]010.Hadoop配置-独立与伪分布式模式.pptx' '[IT18掌www.it18zhang.com]Ambari Hadoop集群管理工具.pptx' '[IT18掌www.it18zhang.com]Spark调优.pptx' '[IT18掌www.it18zhang.com]011.Hadoop配置-完全分布式模式.pptx' '[IT18掌www.it18zhang.com]Avro.pptx' '[IT18掌www.it18zhang.com]ZooKeeper.pptx' '[IT18掌www.it18zhang.com]012.Hadoop Windows下免Cygwin伪分布安装
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值