欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Activity--生命周期

程序员文章站 2022-07-05 08:10:09
...
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity {
    
    private static final String LOG_TAG="VERBOSE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button) findViewById(R.id.start);
        b.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
        Log.v(LOG_TAG, "FirstActivity:onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(LOG_TAG, "FirstActivity:onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v(LOG_TAG, "FirstActivity:onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v(LOG_TAG, "FirstActivity:onResume");
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.v(LOG_TAG, "FirstActivity:onRestart");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v(LOG_TAG, "FirstActivity:onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v(LOG_TAG, "FirstActivity:onStop");
    }
    
}

 
Activity--生命周期
            
    
    博客分类: Android
 

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends Activity {
    
    private static final String LOG_TAG="VERBOSE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Button b = (Button) findViewById(R.id.back);
        b.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Button s = (Button) findViewById(R.id.start3);
        s.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                startActivity(intent);
            }
        });
        Log.v(LOG_TAG, "SecondActivity:onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(LOG_TAG, "SecondActivity:onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v(LOG_TAG, "SecondActivity:onPause");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.v(LOG_TAG, "SecondActivity:onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v(LOG_TAG, "SecondActivity:onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v(LOG_TAG, "SecondActivity:onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v(LOG_TAG, "SecondActivity:onStop");
    }
}

 
Activity--生命周期
            
    
    博客分类: Android

 

1.启动该程序,输出:

FirstActivity:onCreate

FirstActivity:onStart
FirstActivity:onResume

 

2.点击Start SecondActivity,输出:

FirstActivity:onPause

SecondActivity:onCreate

SecondActivity:onStart

SecondActivity:onResume

FirstActivity:onStop

 

3.点击Back to FirstActivity或者按BACK key,输出:

SecondActivity:onPause

FirstActivity:onRestart

FirstActivity:onStart

FirstActivity:onResume

SecondActivity:onStop

SecondActivity:onDestroy

  • Activity--生命周期
            
    
    博客分类: Android
  • 大小: 9 KB
  • Activity--生命周期
            
    
    博客分类: Android
  • 大小: 11.4 KB