Android -- 生命周期(activity跳转过程中的生命周期)

从一个activity跳转到另外一个activity,那么后台的方法生命周期是什么样的呢?

我们再新增一个BMainActivity,与MainActivity一致将其中的每个方法输出加上B*进行观察。

在MainActivity类中增加跳转B的按钮事件。

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

        findViewById(R.id.buttonB).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        startActivity(new Intent(MainActivity.this,
                                BMainActivity.class));
                    }
                });
    }

我们来看效果和输出:
1、进入到应用,点击按钮跳转的B页面
这里写图片描述
输出日志顺序可以看出,在B页面生命周期到onResume的时候,A页面才执行onStop
这里写图片描述

2、点击返回按钮,A页面启动起来后,B页面会stop->destory,可以参照上一篇博客,讲解的home按钮与返回按钮生命周期的变化。http://blog.csdn.net/gaopeng0071/article/details/45026385
这里写图片描述

附上A页面的activity代码

package com.example.helloworld;

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

public class MainActivity extends Activity {

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

        findViewById(R.id.buttonB).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        startActivity(new Intent(MainActivity.this,
                                BMainActivity.class));
                    }
                });
    }

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("AonStrat");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("AonResume");
    }

    protected void onPause() {
        super.onPause();
        System.out.println("AonPause");
    }

    protected void onStop() {
        super.onStop();
        System.out.println("AonStop");
    }

    protected void onDestroy() {
        super.onPause();
        System.out.println("AonDestroy");
    }

    protected void onRestart() {
        super.onRestart();
        System.out.println("AonRestart");
    }

    @Override
    public 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;
    }

}

B页面的activity代码

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class BMainActivity extends Activity {

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

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("BonStrat");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("BonResume");
    }

    protected void onPause() {
        super.onPause();
        System.out.println("BonPause");
    }

    protected void onStop() {
        super.onStop();
        System.out.println("BonStop");
    }

    protected void onDestroy() {
        super.onPause();
        System.out.println("BonDestroy");
    }

    protected void onRestart() {
        super.onRestart();
        System.out.println("BonRestart");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bmain, menu);
        return true;
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值