简体   繁体   English

每次打开活动时都会更新有关活动的信息吗?

[英]Having information on an activity update every time the activity is opened?

Imagine I am in activity 1 and I have a variable x=6 that I display on textView.想象一下,我在活动 1 中,我有一个变量 x=6 显示在 textView 上。 I display it on textView in my onCreate method.我在 onCreate 方法的 textView 上显示它。 In activity 2, I make x = 5. Then, I switch back to activity1.在活动 2 中,我使 x = 5。然后,我切换回活动 1。 The textview will still read x = 6. I understand that I could solve this using Intents, but I'm going to be doing similar things throughout my whole app, so I was wondering if there is an easier way - perhaps a method that updates values every time the activity is opened? textview 仍会读取 x = 6。我知道我可以使用 Intents 解决这个问题,但我将在整个应用程序中做类似的事情,所以我想知道是否有更简单的方法 - 也许是一种更新方法每次打开活动时的值?

Thanks.谢谢。

EDIT: Also, could I refresh each activity each time the back button on a phone is pressed?编辑:另外,每次按下手机上的后退按钮时,我可以刷新每个活动吗?

Create a seperate file where you declare your variables as static and then read from that file throughout your app for all variables.创建一个单独的文件,在其中将变量声明为static ,然后在整个应用程序中从该文件中读取所有变量。 this is not a great solution, but for your scenario it is the simplest/easiest.这不是一个很好的解决方案,但对于您的场景,它是最简单/最简单的。

Also, could I refresh each activity each time the back button on a phone is pressed?另外,我可以在每次按下手机上的后退按钮时刷新每个活动吗?

yes, put your update code in OnResume (it's an override function)是的,将您的更新代码放在OnResume (这是一个覆盖功能)

覆盖 Activity 类的onResume方法以更新文本视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM