Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 342
Eugene Avatar asked Apr 11 '11 12:04

Eugene


2 Answers

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.

like image 192
Marcelo Avatar answered Sep 22 '22 20:09

Marcelo


@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) {

    }               
}
like image 22
HemanSuperman Avatar answered Sep 24 '22 20:09

HemanSuperman