Eugene
Eugene

Reputation: 60204

onResume() isn't triggering on TabHost items switching

I have a TabHost with two tabs in it. The first time I switch to the second tab the onResume() method of my second's tab's activity invoked. Then I have an AlertDialog shown and after it disappears the 'onResume()' method isn't called but I really wait for it. I presume that invocation of 'AlertDialog' triggers 'onPause()' method and the 'onResume()' should be called before 'Activity' is actually shown and ready for interaction with user. But in face 'onPause()' isn't called when I switch back to the first tab which is another activity.

Can you advice why the 'onPause()' and 'onResume()' methods aren't called and what methods are called after showing an 'AlertDialog' or switching between tabs?

Upvotes: 5

Views: 5177

Answers (4)

HemanSuperman
HemanSuperman

Reputation: 31

@mthama's answer worked perfectly fine and had declared LocalActivityManager as a local variable at the Tabhost level. However if I wanted just one of the onResume() on a specific tab to fire, I did as follows.

protected void onResume() {
     super.onResume();       
     try {
         if(mLocalActivityManager != null){
             TabActivityOne tabActivity1 = (TabActivityOne) mLocalActivityManager.getActivity("tabId1");
             if(tabActivity1 != null){
                 tabActivity1.onResume();
             }  
         }           
    } catch (Exception e) {

    }               
}

Upvotes: 2

jayzhou215
jayzhou215

Reputation: 111

I have the same problem, and I try this, and it works~~

private OnTabChangeListener TabChangeListener = new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabId) {
        if (tabId.equals("download")){
            mlocalActivityManager.dispatchPause(isFinishing());
            mlocalActivityManager.dispatchResume();
            tabs.setCurrentTab(1);
        }
    }
};

Upvotes: 1

Marcelo
Marcelo

Reputation: 2095

When you create a TabHost to hold Activities, the childrens inside it can't manage their own lifecycled methods (onResume, onPause, onCreated, etc), and the parent (the holder) must do all the managements. I've implemented this behavior by overwriting onPause and onResume in the holder (the Activity that defines the tabhost), like this:

@Override
public void onPause() {
    super.onPause();
    try {
        mlam.dispatchPause(isFinishing());
    } catch (Exception e) {}
}

@Override
public void onResume() {
    super.onResume();
    try {
        mlam.dispatchResume();
    } catch (Exception e) {}
}

Where "mlam" is the LocalActivityManager instance. With it, I think that your onResume/onPause methods gonna be triggered. Hope that this helps you in some way.

Upvotes: 7

AjOnFire
AjOnFire

Reputation: 2878

I presume that invocation of 'AlertDialog' triggers 'onPause()' method and the 'onResume()' should be called before 'Activity' is actually shown and ready for interaction with user

AlertDialog does not affect the Activity's Life Cycle.

Check out the Activity's Life Cycle Flow Chart Here.

When switching between Tabs, if you want a call back method why don't you use a TabChanged Listener

Upvotes: 1

Related Questions