活动方法:onCreate()和onDestroy() [英] Activity methods:onCreate() and onDestroy()

查看:57
本文介绍了活动方法:onCreate()和onDestroy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当首次创建活动时,系统会在第一个方法被杀死时系统调用OnContentChanged()方法作为第一个方法,而系统最后一次调用是OnDetachedFromWindow()方法,但是android docs表示该活动的整个生命周期发生在OnCreate()OnDestroy()之间.为什么?请帮助我理解这些方法之间的区别.

When an activity is created for the first time then system calls the OnContentChanged() method as the first method and last call by system is the OnDetachedFromWindow() method when an activity is killed, but android docs says entire lifetime of an Activity happens between OnCreate() and OnDestroy(). Why? Please help me in understanding difference between these methods.

代码:

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

public class ActivitylifecycleActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onContentChanged() {
        super.onContentChanged();   
        Toast.makeText(getApplicationContext(),"1. onContentChanged()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(getApplicationContext(),"2. onCreate()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart() {
        super.onStart();
        Toast.makeText(getApplicationContext(),"3. onStart()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRestoreInstanceState(Bundle restoreInstanceState) {
        Toast.makeText(getApplicationContext(),"4. onRestoreinstaneState()", Toast.LENGTH_SHORT).show();
        super.onRestoreInstanceState(restoreInstanceState);
    }

    @Override
    public void onRestart() {
        super.onRestart();
        Toast.makeText(getApplicationContext(),"5. onRestart()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostCreate(Bundle onpostcrete) {
        super.onPostCreate(onpostcrete);
        Toast.makeText(getApplicationContext(),"6. onPostCreate()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Toast.makeText(getApplicationContext(),"8. onPostResume()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Toast.makeText(getApplicationContext(),"9. onAttachedToWindow()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWindowFocusChanged(boolean bo) {
        super.onWindowFocusChanged(true);
        Toast.makeText(getApplicationContext(),"10. onWindowFocusChanged()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUserLeaveHint() {
        super.onUserLeaveHint();
        Toast.makeText(getApplicationContext(),"11. onUserLeaveHint()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        ii=0;
        Toast.makeText(getApplicationContext(),"12. onUserInteraction()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Toast.makeText(getApplicationContext(),"13. onSaveInstanceState()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPause() {
        super.onPause();
        Toast.makeText(getApplicationContext(),"14. onPause()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStop() {
        super.onStop();
        Toast.makeText(getApplicationContext(),"15. onStop()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(),"16. onDestroy()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Toast.makeText(getApplicationContext(),"17. onDetachedFromWindow()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Toast.makeText(getApplicationContext(),"18. onConfigurationChanged()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onSearchRequested() {
        super.onSearchRequested();
        Toast.makeText(getApplicationContext(),"19. onSearchRequested()", Toast.LENGTH_SHORT).show();
        return false;
    }
}

在此代码中,在onCreate()方法之前调用onContentChanged(),在onDestroy()之后调用onDetachedFromWindow().为什么?

In this code, onContentChanged() is called before onCreate() method and onDetachedFromWindow() is called after onDestroy(). Why?

推荐答案

onCreate():

活动开始时,将调用onCreate().在活动的生命周期中只能调用一次.

When an activity starts its life onCreate() is called. It is called only once in the lifecycle of an activity.

onDestroy():

onDestroy().在活动的生命周期中也称为一次.

onDestroy() is called when an activity finishes its life cycle. It is also called once in the lifecycle of an activity.

onContentChanged():

每当屏幕的内容视图更改时(由于调用Window.setContentViewWindow.addContentView),都会调用此挂钩.例如,您将新视图添加到活动中,或者想通过调用notifyDataSetChanged()刷新列表.

This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView). For example you add new view to activity or want to refresh the list by calling notifyDataSetChanged().

onDetachedFromWindow():

与活动关联的主窗口已从窗口管理器中分离出来时调用.例如,当当前活动进入后台或另一个活动位于当前活动的前面时,就会调用它.

Called when the main window associated with the activity has been detached from the window manager. For example, it is called when the current Activity goes into background or another activity came infront of current activity.

这篇关于活动方法:onCreate()和onDestroy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆