在不使用onpause()或onstop()的情况下调用ondestroy()的Android场景 [英] Android scenario where ondestroy() is called without onpause() or onstop()

查看:255
本文介绍了在不使用onpause()或onstop()的情况下调用ondestroy()的Android场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我被要求写下在不调用onpause()或onstop()的情况下调用ondestroy()的方案.是否有可能.如果是,请解释.

A few days back I was asked to write down scenarios where ondestroy() is called without onpause() or onstop() being called. Is it possible. If yes please explain.

推荐答案

如果尝试下面的代码,您会发现在onPause()onStop()生命周期回调被跳过的情况下,确实调用了onDestroy()的情况./p>

If you try below code, you will find a scenario where onDestroy() is indeed getting called while onPause() and onStop() lifecycle callbacks are skipped.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        finish();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e("MainActivity", "onDestroy");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.e("MainActivity", "onPause");

    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e("MainActivity", "onStop");

    }

换句话说,如果在onCreate()中创建活动时调用finish(),则系统将直接调用onDestroy().

In other words, if you call finish() while creating the Activity in onCreate(), the system will invoke onDestroy() directly.

这篇关于在不使用onpause()或onstop()的情况下调用ondestroy()的Android场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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