更改选项卡之前执行任务

发布于 2024-10-20 12:38:14 字数 337 浏览 1 评论 0原文

我有一个有 4 个选项卡的 Android 应用程序,前 3 个选项卡接受用户的输入,然后选择第四个选项卡执行一些计算并显示结果。 这与实现的选项卡配合得很好,因此我只需在同一活动中切换视图,因为我可以轻松访问第四个选项卡上的所有输入数据。

我想做的是在选项卡更改时切换活动。我的选项卡布局失去了控制,将它们放在单独的文件中会更容易,与代码相同。

我想将每个选项卡输入的数据保存到单例中,以便我可以从其他活动访问它,但 onTabChangedListener 似乎不是正确的方法,因为选项卡已更改,新活动已启动且视图已消失。

如何执行操作,例如调用在更改选项卡时但在执行此操作之前从当前视图保存用户数据的方法。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素手挽清风 2024-10-27 12:38:14

使用内部 activitiesonPause() 或 onStop() 方法怎么样?你可以在那里保存你的数据。

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

御弟哥哥 2024-10-27 12:38:14

您可以设置 OnTouchListener 来捕获事件并检索索引,如下所示:

@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.
        ...
    }
}

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.
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文