Reputation: 1267
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?
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.
I have overrided nearly all protected functions of the lifecycle and also finish()
. But nothing happened.
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
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
Reputation: 1520
Do it in onStop() Rather than on destroy; It suits your scenario:) Try and tel:) cheers
Upvotes: 0