简体   繁体   中英

How to Execute an activity N times ,Where N is user input

I wanted to execute a particular activity or a fragment which takes data and save it to database,and that activity executes N number of times. Here N(integer) is the given input from the user

If A is an activity which executes activity B multiple time or N number of times, where N is the given input from the user.where activity B takes some user input data and saves in database N number of times.

Eg: Activity A ---> Enter the number of questions _____(8) Button[next]--Executes activity B

Activity B----> Enter the 1st Question ______________________________ Button[Next] When i click Next button, it should save the first question and must repeat activity B 8 times

I believe you want a For loop. Without knowing what code you have, I can only provide psuedo-code.

int userInput = getUserInput();

for(i = 0; i < userInput; i++) {
    // Display the form
    // User inputs data
    // Write it to the database
}

provide a startActivity method with a parameter N for activity B, and use it to pass N from activity A to B, on click of the next button goto activity B again if N is greater than zero and finish current activity to avoid memory leaks and minus N by one each time, this should work, here is some kotlin code

class ActivityB : AppCompatActivity() {

    companion object {
        @JvmStatic
        fun start(context: Context, n: Int) {
            val intent = Intent(context, ActivityB::class.java)
            intent.putExtra("N", n)
            context.startActivity(intent)
        }

        lateinit var n: Int

        .
        .
        .

        override fun onCreate(savedInstanceState: Bundle?) {
            ...
            n = intent.getIntExtra("N", 0)

            nextButton.setOnClickListener {
                if( n > 0 ) {
                    ActivityB.startActivity(this, n-1)
                } else {
                    // do what you need to do
                }
                activity?.finish()
            }
            ...
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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