onResume() isn't triggering on TabHost items switching
Asked Answered
N

4

5

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?

Natiha answered 11/4, 2011 at 12:54 Comment(0)
T
1

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

Tie answered 11/4, 2011 at 13:6 Comment(0)
D
7

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.

Denbighshire answered 28/3, 2012 at 21:29 Comment(1)
And, LocalActivityManager mlam = new LocalActivityManager(ParentActivity.this, false); mlam.dispatchCreate(savedInstanceState); yourTabHost.setup(mlam); Anyway nice answer mthamaSouthern
E
2

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

    }               
}
Extrorse answered 1/3, 2014 at 5:7 Comment(1)
To the person who left a negative rating, I would have welcomed a feedback on why it did not work.Extrorse
T
1

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

Tie answered 11/4, 2011 at 13:6 Comment(0)
K
1

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);
        }
    }
};
Kamala answered 17/5, 2012 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.