not run oncreate() ,on tab change in same page, 2nd time

后端 未结 2 1729
独厮守ぢ
独厮守ぢ 2021-01-23 22:07

I have 4 tabs in a page view when i click first time on tab it executes it oncreate() method of corresponding tab and when i go to other tab on same page and again

2条回答
  •  无人及你
    2021-01-23 22:25

    Tabs that contain activities are implemented by means of ActivityGroup. When someone changes a tab, corresponding activity is created only if necessary. When you move to any tab for the second time, the activity is already existing and only its window is shown. You should use instead:

    TabHost.setOnTabChangedListener(TabHost.OnTabChangeListener l)
    

    TabActivity with separate activities as a content are a little bit tricky so maybe you should consider using Views instead. If not you can use the following code to access all activities from each other:

    get instances of content activities from TabActivity:

    TabActivity.getLocalActivityManager().getActivity(String name)
    

    where name is given in newTabSpec() method.

    get instance of TabActivity from content activities:

    FirstTab.getParent()
    

    Using these method you can create communication between all activities (using proper casting). For example:

    public void onTabChanged(String label) {
    
      if(label.equals("Activity2")) {
          SecondActivity sa = (SecondActivity) getLocalActivityManager().getActivity(label);
          sa.modifySomething();
      }
    }
    

    If you want to change activities under tabs you should use:

    tabHost.clearAllTabs ()
    

    and create all TabSpecs once again.

提交回复
热议问题