更改选项卡之前执行任务
我有一个有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用内部
activities
的onPause
() 或onStop
() 方法怎么样?你可以在那里保存你的数据。what about using
onPause
() oronStop
() methods of the inneractivities
? you could save your data there.您可以设置 OnTouchListener 来捕获事件并检索索引,如下所示:
You could set an OnTouchListener to catch the event and retrieve the index like this: