Android 中如何区分切换 Activity 和切换到主屏幕
我正在开发 Android:: 媒体播放器应用程序。为简单起见,该应用程序有 3 个活动:A、B 和 C 是活动。
当我从活动 A 切换到活动 B 时:活动 A 的 onPause() 和 onStop() 被调用。但是当我从 Activity A 进入主屏幕时,会调用相同的函数:onPause() 和 onStop()。
目前我面临的问题是:如果用户进入主屏幕::室外能见度、亮度等设置必须重置到系统,另一方面,如果我在活动之间切换,我必须维护这些值根据具体应用。
我如何区分,我正在同一应用程序的活动之间切换(或)进入主屏幕。
I am working on Android:: Media Player Application. The application has 3 activities for simplicity sake:: A, B and C are the activities.
When I am switching from Activity A to B: onPause() and onStop() of Activity A are getting called. But when I go to home screen from Activity A, the same functions: onPause() and onStop() are getting called.
Currently I am facing a problem that: If user goes to home screen:: The settings like outdoor visibility, brightness etc has to be reset to system and on the other hand, If I am switching between the activities, I have to maintain the values as per application specific.
How can I distinguish that, I am switching between the activities of the same application (or) going to home screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次活动出现在屏幕上时,您都应该进行这些更改,并在活动从屏幕上消失时恢复设置。活动应该像单独的模块一样,因此减少它们的依赖性是一件好事。
最好通过创建模板活动来完成此操作。在
onResume()
和onPause()
方法中进行所有必要的更改,并从此方法派生所有其他活动。这样,您的设置管理代码将在每个活动中自动调用。至于一般的应用程序状态,你无法真正检测到你的应用程序是否出现在屏幕上,因为Android中的应用程序只是一堆独立的组件(活动、服务、内容提供者和广播接收器)。没有任何一个应用程序本身具有特定的可见性状态。只有活动才有它,彼此独立。
You should make these changes every time when activities come to the screen and restore the settings when they disappear from the screen. Activities are supposed to be like separate modules, so it's a good thing to make them less dependent.
This is best done with creating a template activity. Make all the necessary changes in the
onResume()
andonPause()
methods and derive all other activities from this one. This way your settings management code will be called automatically in every activity.As for the general application state, you can't really detect whether your application is present on the screen or not because an application in Android is just a bunch of independent components (activities, services, content providers and broadcast receivers). There is no single application with particular visibility states per se. Only activities have it, independently of each other.
我认为解决方案是检测用户何时按下主页按钮。 方法来完成此操作。
您可以通过重写Activity 类的
I think the solution is to detect when the user presses the HOME button. You can do this by overriding the method
of the Activity Class.
也许您可以使用 SharedPreferences 来存储应用程序特定的设置当您的应用程序正在使用时。并始终在 onStop() 中恢复
Perhaps you could use SharedPreferences to store the app specific settings while your app is in use. And always restore then in onStop()