简体   繁体   English

在TabHosts子活动中未调用onDestroy或onPause

[英]onDestroy or onPause not called in a TabHosts child activity

I have a Tabhost and in that tabhost there are 4 tabs and in every tab there is an activity. 我有一个Tabhost,在该Tabhost中有4个选项卡,每个选项卡中都有一个活动。 When an activity lose focus i want to catch it with onDestroy or onPause method. 当某项活动失去焦点时,我想使用onDestroy或onPause方法抓住它。

What I have tried? 我尝试了什么?

  1. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - this method calls onDestroy of the child activity when a tab gain focus (?!*%>) and then calls onCreate of the child activity. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) -当选项卡获得焦点(?!*%>)时,此方法调用子活动的onDestroy,然后调用子活动的onCreate。

  2. I have overrided nearly all protected functions of the lifecycle and also finish() . 我已经覆盖了生命周期中几乎所有受保护的函数,还覆盖了finish() But nothing happened. 但是什么也没发生。

  3. I have tried TabHosts onTabChanged function but i cant retrieve any activity from tabhost and i cant call any finish() -like function so it failed too. 我已经尝试过TabHosts onTabChanged函数,但是我无法从tabhost检索任何活动,也无法调用任何类似于finish()的函数,因此它也失败了。

Please help me with this situation!! 请在这种情况下帮助我!! Thanks in advance. 提前致谢。

EDIT: 编辑:

By the way I am extending Activity not TabActivity. 顺便说一句,我扩展活动而不是TabActivity。

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        Activity currentActivity = getCurrentActivity();
        if (currentActivity instanceof TheActivity) {
            // do nothing, we are here!
        }
        else { 
            ((TheActivity) currentActivity).methodToStopTimer();
        }
    }
});

or the most common way 或最常见的方式

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        Activity currentActivity = getCurrentActivity();
        if (!currentActivity instanceof TheActivity) {
            ((TheActivity) currentActivity).methodToStopTimer();
        }
    }
});

EDIT: from top of my head I thought that method was part of TabHost, but never mind, just check the tabId so you know which activity you are in: 编辑:从我的头顶上我以为该方法是TabHost的一部分,但没关系,只需检查tabId,以便您知道自己处于哪个活动中:

if(!tabId.equals("idForYourActivity")){
    ActivityName.methodToStopTimer();
}

You will need to have the methodToStopTimer() to be static. 您将需要methodToStopTimer()是静态的。

Do it in onStop() Rather than on destroy; 在onStop()而不是在destroy上执行; It suits your scenario:) Try and tel:) cheers 适合您的情况:)尝试并致电:)欢呼

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

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