Tuna Karakasoglu
Tuna Karakasoglu

Reputation: 1267

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. When an activity lose focus i want to catch it with onDestroy or onPause method.

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.

  2. I have overrided nearly all protected functions of the lifecycle and also 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.

Please help me with this situation!! Thanks in advance.

EDIT:

By the way I am extending Activity not TabActivity.

Upvotes: 0

Views: 1570

Answers (2)

Carnal
Carnal

Reputation: 22064

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:

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

You will need to have the methodToStopTimer() to be static.

Upvotes: 1

Manoj Kumar
Manoj Kumar

Reputation: 1520

Do it in onStop() Rather than on destroy; It suits your scenario:) Try and tel:) cheers

Upvotes: 0

Related Questions