-2

I have 5 activities in my project, where one activity leads to another in the following manner:

  1. Selection
  2. SelectType
  3. SelectMinBudge
  4. SelectMaxBudget
  5. SittingType

Every selected item is a String from each activity.

From each activity, the user selects one option from the DropDownList, and moves forward to the next activity. I want to gather all 5 variables at the last activity (Landing). I need to know some efficient method to do this.

Screenshot of Project classes

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
  • put data to bundle than pass it to next activity with Intent – Vigen May 12 '19 at 11:46
  • First solution: You could create a singleton class to hold data. At the end retrieve the saved data perform your operations. Another option is to gradually pass intent and load the data from intent at the end. – Taseer May 12 '19 at 13:51
  • @Vigen I have used this method and it is working. – Raja Shahroze May 12 '19 at 23:26
  • @TaseerAhmad this is more efficient. Worked for me. – Raja Shahroze May 12 '19 at 23:27
  • the singletone is not right choice in this case. Also there are problems with static data in Android (Android will shut down your app's jvm when resources are low and you will lose static data if you don't care of them). – Vigen May 13 '19 at 21:34

1 Answers1

0

I use Kotlin so the example code will be in Kotlin only, but I will try to comment out each line of code.

Solution 1: Create a SingleTon class to hold data and use its data on at ending activity.

class DataHolder {    //public class

   companion object { //Equavilent to Java static.
     var variable1 = 0   
     var variable2 = "Some string"  //Define your static variables.
     .... 
  }
}

Access this Singleton class in your Activity1 -> Activity2 and so on. When you reach your ending activity, simple reaccess this class and retrieve the respective data stored.

Pro: Less repeated Intent code in your activities.
Cons: Obviously static variables are permanent unless it's class loader goes out of scope. Since the data is very little, nothing too much to worry about.

Solution 2: Create intents, load data and gradually pass it on to your activities and retrieve the data at the ending activity. The only downside is that you would have to take extra care to repeatedly insert data, grab the data with the respective key. Maybe 'error' prone unless you stay extra cautious.

Taseer
  • 3,143
  • 3
  • 13
  • 31