0

My problem is that I have created arrayof images and always randomly let the screen to display 2 of these images and after pressing next_button and next show 2 images but when I turn the screen so it all resets. I know the activity will change and it has to be saved "onsaveinstancestate" but I don't know anymore could someone please send me a solution. Ps. sorry my code looks so bad because I start with the kotlin and OOP.

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_roll.*
import kotlinx.android.synthetic.main.activity_roll0.*
import java.lang.NullPointerException
import kotlin.random.Random

class RollActivity0 : AppCompatActivity() {

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

        //init array
        var cards = arrayOf(R.drawable.bart_cassidy,
                                        R.drawable.black_jack,
                                        R.drawable.calamity_janet,
                                        R.drawable.el_gringo,
                                        R.drawable.jesse_jones,
                                        R.drawable.jourdonnais,
                                        R.drawable.kit_carlson,
                                        R.drawable.lucky_duke,
                                        R.drawable.paul_regret,
                                        R.drawable.pedro_ramirez,
                                        R.drawable.rose_doolan,
                                        R.drawable.sid_ketchum,
                                        R.drawable.slab_the_killer,
                                        R.drawable.suzy_lafayette,
                                        R.drawable.vulture_sam,
                                        R.drawable.willy_the_kid)

        val random_index = java.util.Random()
        var index = 0

        //shuffling array
        for (i in cards.size - 1 downTo 1)
        {
            val j = random_index.nextInt(i + 1)
            val tmp = cards[i]
            cards[i] = cards[j]
            cards[j] = tmp
        }

        imageView3.setImageResource(cards[index])
        index++ //next image
        imageView4.setImageResource(cards[index])
        index++ //next image

        next_button1.setOnClickListener {
                if ( index >= cards.size )//if i am on end array make new shuffled array
                {
                    //shuffling array
                    for (i in cards.size - 1 downTo 1)
                    {
                        val j = random_index.nextInt(i + 1)
                        val tmp = cards[i]
                        cards[i] = cards[j]
                        cards[j] = tmp
                    }

                    index = 0 //new start
                }

                imageView3.setImageResource(cards[index])
                index++

                imageView4.setImageResource(cards[index])
                index++
        }
    }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Adam Barča
  • 51
  • 1
  • 10
  • 1
    Side-note: Consider using `Collections.shuffle(yourArrayList);` to shuffle an arrayList instead of what you're doing here. Here's a link to some documentation: https://www.tutorialspoint.com/java/util/collections_shuffle.htm. As for the main question, you could possibly (haven't tested it) pass the arrayList using the savedInstanceState logic and the two indexes of the images currently shown in your imageViews when the activity is rotated. Here's a stack post which i think may be useful in your case : https://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please – Stelios Papamichail Feb 06 '20 at 10:37
  • 1
    Thanks for your advice. – Adam Barča Feb 06 '20 at 23:33

2 Answers2

0

Just add android:configChanges="orientation|keyboardHidden" in Manifest file in your activity.

<activity android:name=".RollActivity0"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">
Deepak Ror
  • 2,084
  • 2
  • 20
  • 26
0

If I understand you right, you don't want the two images to change when turning the screen right? That for you can safe which images are currently displayed in onSaveInstanceState and read that stored information in onCreate .

Safe your index when setting pictures

...your code...
var indexPictureOne = -1
var indexPictureTwo = -1
imageView3.setImageResource(cards[index])
indexPictureOne = index
index++ //next image
imageView4.setImageResource(cards[index])
indexPictureTwo = index
index++ //next image
...your code...

Store the current state

override fun onSaveInstanceState(bundle: Bundle) {
   super.onSaveInstanceState(bundle);
   icicle.putInt("pictureOneIndexKey", indexPictureOne)
   icicle.putInt("pictureTwoIndexKey", indexPictureTwo)
}

Restore the state when activity is created again

override fun onCreate(bundle: Bundle?){
   val restoredIndexPictureOne = bundle?.getInt("pictureOneIndexKey",null)
   val restoredIndexPictureTwo = bundle?.getInt("pictureTwoIndexKey",null)

   ... //if restoredIndexPictureOne and restoredIndexPictureTwo are not null 
   skip your random logic and use these indexes, else use your random logic to 
   get two new random pictures

   ...your code..
}

I hope I got your question right and could help a bit!

Erik Schmidt
  • 85
  • 1
  • 8