开发者

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

开发者 https://www.devze.com 2023-03-15 08:15 出处:网络
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 click on previous clicked tab then i

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 click on previous clicked tab then its oncreate() method not execute why?why it is not work as button click means each time i click its oncreate() method run.

my code for tab activity class is below

  intent1 = new Intent().setClass(this, keywordxmlparsing.class);
spec1 = tabHost.newTabSpec("Activity2").setIndicator("keyword/ search...").setContent(intent1);
tabHost.addTab(spec1); 

intent2 = new Intent().setClass(this, filter.class);
spec2 = tabHost.newTabSpec("Activity1").setIndicator("filter search").setContent(intent2);
tabHost.addTab(spec2);
intent3 = new Intent().setClass(this, OpeningToday.class);
spec3 = tabHost.newTabSpec("Activity3").setIndicator("opening today").setContent(intent3);
tabHost.addTab(spec3);
intent4 = new Intent(keywordresulttab.this,Map.class);
spec4 = tabHost.newTabSpec("Activity4").setIndicator("Map").setContent(intent4);
tabHost.addTab(spec4);

dear if i use

    @Override
public void onTabChanged(String label) {
    // TODO Auto-generated method stub
    if(label == "Activity2") {




    }
    try{
    if(label == "Activity4") {
        Intent intent4; 
        intent4 = new Intent(keywordresulttab.this,Map.class);
        startActivity(intent4);
        Log.i("suiuawhd","maps class");




                }

then can i go to other activity means using

  intent4 = new Intent(keywordresulttab.this,Map.class);
            startActivity(intent4);

we can load again activity on tab click??then what to written in place of

     tabHost.addTab(spec3);
intent4 = new Intent(keywordresulttab.this,Map.class);
spec4 = tabHost.newTabSpec("Activity4").setIndicator(开发者_JAVA技巧"Map").setContent(intent4);
tabHost.addTab(spec4);


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.


In the android oncreate() lifecycle called once in life of the activity (like constructor), as you press again that time the activity is alive and in memory so it will not call onCreate again.

so you have to implement

TabHost.setOnTabChangedListener(TabHost.OnTabChangeListener l).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号