我正在 Android Studio 中开发一个应用程序,但在活动之间导航和维护用户在活动中所做的更改时遇到问题。
我想要什么:
第 1 步:用户在活动 1 中引入数据
第 2 步:用户转到活动 2
第 3 步:用户在活动 2 中引入数据
第 4 步:用户返回活动 1
第 5 步:用户更改活动 1 中的数据
第 6 步: 用户转到“活动 2”,但数据已填满(由于第 3 步)
每个活动有 2 个按钮:一个用于返回(上一个活动),一个用于前进(下一个活动)。后退按钮只是调用方法 onBackPressed()
,而前进按钮则启动下一个 Activity,如下所示:
val i = Intent(this@NActivity, N+1Activity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(i)
从 Activity 2 到 Activity 1 它维护数据,但是从 Activity 1 到 Activity 2 它维护数据不是。我认为,当按下后退按钮时,它正在完成 Activity 2,或者当第二次单击前进按钮时,它正在启动新的 Activity 2。
我已经尝试使用 moveTaskToBack(true)
但它最小化了整个应用程序。
I am developing an app in Android Studio and I am having trouble navigating between activities and maintaining the changes users make in them.
What I want:
Step 1: User introduces data in Activity 1
Step 2: User goes to Activity 2
Step 3: User introduces data in Activity 2
Step 4: User goes back to Activity 1
Step 5: User changes data in Activity 1
Step 6: User goes to Activity 2 but the data is already filled (because of step 3)
Each activity has 2 buttons: one to go back (previous activity), one to go forward (next activity). The back button is just calling the method onBackPressed()
and the forward button is starting the next activity like this:
val i = Intent(this@NActivity, N+1Activity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(i)
From Activity 2 to Activity 1 it maintains the data, however from Activity 1 to Activity 2 it does not. What I perceive is that it is either finishing the Activity 2 when the back button is pressed or it is starting a NEW Activity 2 when the forward button is clicked for the second time.
I have already tried to use moveTaskToBack(true)
but it minimizes the whole app.
发布评论
评论(2)
您需要了解活动不在后台运行。用户从 Activity 导航后发生的任何情况都取决于系统
有多种方法可以实现您想要做的事情,但我会给您两种
假设我们有一个名为age的整数,我们想要更改
在普通的 kotlin 类中说 Common.kt
在你的 Activity1.kt
在你的第二个活动 Activity2.kt
处这一点。如果我们回到Activity1,age的值仍然是10,我们可以根据需要更改它
片段更改活动中的变量值
从Activity1.kt 中的
通过将入口点作为活动并将其他活动更改为片段,您可以 然后从片段中修改这个变量,如下所示
You need to understand that activities don't run in the background. Whatever happens to an activity after the user navigates from it depends on the system
There are several ways of achieving what you want to do but I'll give you two
Assuming we have an integer called age that we want to change
in a normal kotlin class say Common.kt
In your Activity1.kt
In your second activity Activity2.kt
At this point. If we go back to Activity1, the value of age will still be 10 and we can change it as needed
By having the entry point as an activity and changing the other activities to fragments, you can change the value of the variable in the activity from the fragments
in Activity1.kt
You can then modify this variable from a fragment like so
活动仅用于存储瞬态 UI 状态,并且一次只有一个活动处于活动状态。如果您启动另一项活动,则原始活动可能会或可能不会被杀死,并且最好假设它会被杀死(有一个开发人员设置“不保留活动”,允许您检查您的行为是否正确)。
在您的示例中,如果您希望对 Activity 2 所做的状态更改在用户下次返回 Activity 2 时可用,那么它们需要保存在某处(例如内存存储、文件存储或一个数据库)。创建活动后,您需要检查持久性并更新活动以显示正确的信息。
我建议阅读一些有关活动和活动生命周期的内容:
https:/ /developer.android.com/guide/components/activities/intro-activities
https://developer.android.com/guide/components/activities/activity-lifecycle
我还建议阅读有关片段的内容:
https://developer.android.com/guide/fragments
一旦你熟悉了这些,我建议你研究一下 android ViewModel 类和实时数据:
<一个href="https://developer.android.com/topic/libraries/architecture/viewmodel" rel="nofollow noreferrer">https://developer.android.com/topic/libraries/architecture/viewmodel
< a href="https://developer.android.com/topic/libraries/architecture/livedata" rel="nofollow noreferrer">https://developer.android.com/topic/libraries/architecture/livedata
https://developer.android.com/guide/components/activities/activity-lifecycle#saras
最后一点,其中一些内容将随 Compose 一起消失,但如果您刚刚开始,我现在不会担心:
https://developer.android .com/jetpack/compose
Activities are only intended to store transient UI state and only one activity at a time is active. If you start another activity the original one may or may not get killed and its good practice to assume that it will (there is a developer setting "Dont keep activities" that allows you to check yours behave correctly).
In your example, if you want the state changes that were made to Activity 2 to be available the next time the user comes back to Activity 2 then they need to be persisted somewhere (for example to an in memory store, a file store, or a database). When the activity is created you would need to check that persistance and update the activity to display the correct information.
I'd suggest doing some reading around the activies and the activity lifecycle:
https://developer.android.com/guide/components/activities/intro-activities
https://developer.android.com/guide/components/activities/activity-lifecycle
I'd also suggest reading about Fragments:
https://developer.android.com/guide/fragments
Once you're familiar with those I'd suggest looking into the android ViewModel class and Live Data:
https://developer.android.com/topic/libraries/architecture/viewmodel
https://developer.android.com/topic/libraries/architecture/livedata
https://developer.android.com/guide/components/activities/activity-lifecycle#saras
As a final note, some of this stuff is going to go away with Compose, but if you're just starting out I wouldn't worry about that right now:
https://developer.android.com/jetpack/compose