简体   繁体   English

OnCreate方法不断调用OnResume方法

[英]OnCreate method keeps calling OnResume method

So i have this app and everytime i start the main activity the OnResume method gets called.i have put a Toast message in both the OnCreate and OnResume methods and everytime i start the Activity both Toasts show. 所以我有这个程序,并每次我启动主活动OnResume方法获取called.i已经把Toast两个消息OnCreateOnResume方法,每次我开始的ActivityToasts演出。 Is there something i am missing here? 我在这里缺少什么吗? I have looked at the code line by line but found nothing that might be causing this. 我逐行查看了代码,但没有发现任何可能导致此问题的原因。

OnResume() is called everytime in the lifecycle of an activity. 在活动的生命周期中每次都调用OnResume()。 See the picture below. 参见下图。 Add onResume() only when you need to do some task if app is to started from background or from paused state. 仅当应用要从后台或暂停状态启动时,才需要在执行某些任务时添加onResume()。 在此处输入图片说明

You need to create some condition and call recreate only if that condition is satisfied. 您需要创建一些条件,仅在满足该条件时才调用recreate。 assign a boolean variable if your condition is satisfied and call recreate if this boolean is true or false as per your condition, Otherwise your loop will never stop. 如果您的条件满足,则分配一个布尔变量,如果根据条件该布尔值为true或false,则调用recreate,否则循环将永远不会停止。

private boolean isCheck = false;
onCreate() {
    if(somecondition == true) {
        isCheck = true;
    }
}

onResume() {
    if(!isCheck){
        recreate();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM