Home > Software engineering >  Android: how to navigate between activities and keep its states
Android: how to navigate between activities and keep its states

Time:03-01

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.

CodePudding user response:

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

CodePudding user response:

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

  1. Using a static variable. in a normal kotlin class say Common.kt
    class Common{
    
    companion object{
    var age=1
    }
    }

In your Activity1.kt

 class Activity1:AppCompatActivity(){

override fun onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)

//change your value as required
Common.age=4
}
}

In your second activity Activity2.kt

class Activity2:AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)

//Since age is static. It's value still remains as last updated even though the activity has changed
//change your variable again as required
Common.age=10

}
}

At this point. If we go back to Activity1, the value of age will still be 10 and we can change it as needed

  1. Using fragments 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

class Activity1:AppCompatActivity(){

//This is your variable
var age:Int=1
override fun onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)


}
}

You can then modify this variable from a fragment like so

class Fragment1: Fragment(){
 
fun changeActivityVarValue(){

val activity1= requireActivity() as Activity1

activity1.age=10
}
}
  • Related