开发者

Perform task before changing tabs

I have and android app that has 4 tabs, the first 3 take input from the user and selecting the 4th tab performs some calculations and displays results. This works fine with the tabs implemented so that I just switch views within the same activity as I can easily access all of the inputed data on the 4th tab.

What I would like to do is switch activities when the tabs are changed. My tab layouts were getting out of control and it is easier having them in separate fies, same with the code.

I would like to save the inputed data from each tab to singleton so I can access it from other activities but onTabChangedListener does not seem to be the开发者_如何学C way go as the tab has changed, new activity started and view gone already.

How can I perform an action like calling a method that saves user data from the current view when a tab is changed but BEFORE it does it.


what about using onPause() or onStop() methods of the inner activities? you could save your data there.


You could set an OnTouchListener to catch the event and retrieve the index like this:

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getX();
        int width = v.getWidth();

        int index = (int) (x / (width / tabsCount)); // The amount of tabs.
        ...
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜