开发者

onTabChanged(first_page) is always called, is this expected behavior?

I have TabActivity with 3 tabs. I save last selected page number to shared

preference and get it back onResume to call tabHost.setCurrentTab().

But onTabChanged() is called twice when selecting other than first

page.

  • Calling tabHost.setCurrentTab(0) makes onTabChanged called once. This is expected.
  • Calling tabHost.setCurrentTab(1 or above) makes onTabChanged called twice, specifying first page and the expected page. This is not what I expected.

I'm running it on OS2.3.3.

Is this expected (designed) behavior? (I prefer onTabChanged will be called only once to

prevent unwanted processing for first page. If this is designed behavior I need to think

some logic to ignore unwanted call to onTabChanged.)

EDIT

Code fragment in onCreate().

tabHost = getTabHost();
tabHost.setOnTabChangedListener(new TabChangedListener());
TabSpec firstTab = tabHost.newTabSpec("First")
    .setIndicator(firstButton)
    .setContent(R.id.first_content);
tabHost.addTab(firstTab);
TabSpec secondTab = tabHost.newTabSpec("Second")
    .setIndicator(secondButton)
    .setContent(R.id.second_content);
tabHost.addTab(secondTab);
TabSpec thirdTab = tabHost.newTabSpec("Third")
    .setIndicator(thirdButton)
    .setContent(R.id.third_content);
tabHost.addTab(thirdTab);

And code in onResume().

int tabIndex = PreferenceManager.getDefaultSharedPreferences(this).getInt(ADDRESS_TAB_KEY,0);
tabHost.setCurrentTab(tabIndex);

And OnTabChangeListener.

class TabChangedListener implements OnTabChangeListener
{
    public void onTabChanged(String tabId)
    {
        Log.d(TAG,"onTabChanged " + tabId);

        if(tabId.equals("First") == true)
        {
            setupView(0);
        }
        else if(tabId.equals("Second") == true)
        {
            setupView(1);
        }
        else if(tabId.equals("Third") == true)
        {
            setupView(2);
        }
    }
}

I found that onTabChanged() is called before onResume() is called! When I moved a line calling tabHost.setOnTabChangedListener() after calling tabHost.addTab(), onTabChanged() was not called from onCreate() which seems understandable. But, in this case, calling tabHost.setCurrentTab(0) won't call onTabChanged at all! This is ridiculous. So I ended up with this solution.

1) Call tabHost.setOnTabChangedListener after calling tabHost.addTab().

2) Twisted onResume() code as shown below.

int tabIndex = PreferenceManager.getDefaultSharedPreferences(this).getInt(ADDRESS_TAB_KEY,0)开发者_如何学C;
if(tabIndex == 0)
{
    setupView(0);   // setup view since onTabChanged won't be called
}
else
{
    tabHost.setCurrentTab(tabIndex);
}

Now I have my solution and questions are:

  • Is this an expected behavior?
  • Is there any better solution for this?

Thanks for your attention.


We have no code so it's kinda hard to tell the exact reason but you most probably are calling setCurrentTab from you onCreate too. When you get back to the activity you call first the setCurrentTab in the onCreate method, and after that the setCurrentTab in the onResume method gets called.

If that's not the problem you gonna have to share some code to lets a help.

Actually, you should always share relevant portions of code or it gets really difficult to find the real problems.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜