-3

i am learning android studio, and i have a question that might be the basic of android studio.

Why doesn't it run from top to bottom like java in eclipse? For example, i have below code:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.game)

        var rank2 = mutableListOf(R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4)
        val rank1: MutableList<Int> = ArrayList()
        var imageView1 = findViewById<ImageView>(R.id.imageView1);
        var imageView2 = findViewById<ImageView>(R.id.imageView2);

        imageView1.setImageResource(rank2[0])
        imageView2.setImageResource(rank2[1])

        var x = 0
        Log.d("log", "")
        imageView1.setOnClickListener{
            if (x < 2) {
                rank1.add(rank2[2 * x])
                if(x<2) {
                    imageView1.setImageResource(rank2[2 * (x + 1)])
                    imageView2.setImageResource(rank2[2 * (x + 1) + 1])
                    x++
                }
            }
        }
        imageView2.setOnClickListener{
            if (x < 2) {
                rank1.add(rank2[2 * x + 1])
                if(x<2) {
                    imageView1.setImageResource(rank2[2 * (x + 1)])
                    imageView2.setImageResource(rank2[2 * (x + 1) + 1])
                    x++
                }
            }
        }

        Log.d("done", "done")


        //do something after finish above code.
    }

as soon i run the program, the log "done" shows. what should i do if i want my program to first run the loop, then do another command or function?

Thank you

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
jun
  • 23
  • 3
  • What did you expect to happen in this code? You are inflating a view and setting two imageviews. Other than that are you expecting something else to happen before "done" log? – tom Dec 01 '20 at 06:59
  • the log "done" actually runs at last. its not running before any code. – MRazaImtiaz Dec 01 '20 at 07:05
  • 1
    First thing first, Android studio is development toolkit (IDE) and not the runtime. So, it's not responsible to run your android app. Second thing, you need to learn about activity lifecycle. eventually apps in android run on JVM same as other java programs although it has it's own set of rules on execution following some lifecycle methods. – Jeel Vankhede Dec 01 '20 at 07:08
  • It does run sequentially within the function. The Android OS has a [lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle) which might be causing you confusion. For instance you won't not see the UI effects until later. – user650881 Dec 01 '20 at 07:35

2 Answers2

0

Why doesn't it run from top to bottom like java in eclipse?

In short, the sequence of execution in an Android Application is usually based on the Lifecycle callbacks.

In your example, I believe you are using an Activity, which is an app component that has 6 callbacks. Each app component has it's Lifecycle. The code, which you have showed, consist your handling of onCreate(), in your case, a lifecycle callback, which is called when the activity is starting.

what should i do if i want my program to first run the loop, then do another command or function?

Ideally, you want to have a look at what each Lifecycle callbacks does and figure out where to place your functionalities.

For example: In onCreate, you would do your most of your initialization.

I would suggest you to try stepping through statements via the debugger. I feel it might give you a better picture of what's going on.

Apart from it, your Log statement related to Log.d("log", "") should appear in the Logcat as well. Also the android documentation recommends a convention: To declare a TAG constant in your class and use it as the Log Tag. Then in your Logcat, you can filter the Logs via your TAG.

Kalai
  • 503
  • 1
  • 3
  • 12
0

Your code is running in the order that you have written it. You have some confusion about what the following code is actually doing

imageView1.setOnClickListener{
    if (x < 2) {
        rank1.add(rank2[2 * x])
        if(x<2) {
            imageView1.setImageResource(rank2[2 * (x + 1)])
            imageView2.setImageResource(rank2[2 * (x + 1) + 1])
            x++
        }
    }
}
imageView2.setOnClickListener{
    if (x < 2) {
        rank1.add(rank2[2 * x + 1])
        if(x<2) {
            imageView1.setImageResource(rank2[2 * (x + 1)])
            imageView2.setImageResource(rank2[2 * (x + 1) + 1])
            x++
        }
    }
}

What you are doing is actually listening for the click event to be triggered on either imageView1 or imageView2 and when it is triggered, your code will be executed. This is commonly known as Event-driven programming.

What your code is doing is actually correct and would be better understood if you look at it like this

1. initialize some variables

2. find views by their id

3. attach click listeners to the views

4. print the log statement
Bilal Naeem
  • 962
  • 7
  • 13