Search code examples
androidkotlinandroid-activity

Android: how to navigate between activities and keep its states


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.


Solution

  • 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
    }
    }