Send and receive extras with instance

main
Alexis Drai 3 years ago
parent 25806712b3
commit af21a58dee

@ -7,4 +7,11 @@
<component name="ProjectType">
<option name="id" value="Android" />
</component>
<component name="VisualizationToolProject">
<option name="state">
<ProjectState>
<option name="scale" value="0.18554938007695596" />
</ProjectState>
</option>
</component>
</project>

@ -20,6 +20,7 @@
android:value="" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
@ -30,6 +31,8 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

@ -1,11 +1,24 @@
package fr.iut.pm
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class CheatActivity : AppCompatActivity() {
private var cheatAnswer: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.cheat_activity)
with(intent)
{
cheatAnswer = getBooleanExtra(CHEAT_ANSWER, false)
}
findViewById<Button>(R.id.btnCheat).setOnClickListener {
findViewById<TextView>(R.id.textViewAnswerCheat).text = cheatAnswer.toString()
}
}
}

@ -13,11 +13,13 @@ import fr.iut.pm.model.TrueFalseQuestion
const val TAG = "MyQuizActivity"
const val QUESTION_INDEX: String = "questionIndex"
const val CHEAT_ANSWER: String = "cheatAnswer"
class QuizActivity : AppCompatActivity() {
private var questionIndex: Int = 0
private var cheatAnswer: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
Log.println(Log.INFO, TAG, "Creating...")
@ -31,6 +33,7 @@ class QuizActivity : AppCompatActivity() {
with(savedInstanceState) {
Log.println(Log.WARN, TAG, "LOADING...")
questionIndex = getInt(QUESTION_INDEX)
cheatAnswer = getBoolean(CHEAT_ANSWER)
}
}
@ -42,6 +45,7 @@ class QuizActivity : AppCompatActivity() {
findViewById<Button>(R.id.btnCheat).setOnClickListener {
val cheatIntent = Intent(this, CheatActivity::class.java)
cheatIntent.putExtra(CHEAT_ANSWER, cheatAnswer)
startActivity(cheatIntent)
}
}
@ -77,6 +81,7 @@ class QuizActivity : AppCompatActivity() {
outState.run {
println("saving idx: $questionIndex")
putInt(QUESTION_INDEX, questionIndex)
putBoolean(CHEAT_ANSWER, cheatAnswer)
}
super.onSaveInstanceState(outState)
}
@ -101,11 +106,17 @@ class QuizActivity : AppCompatActivity() {
showQuestion(textView, questions)
}
private fun storeAnswer(answer: Boolean) {
cheatAnswer = answer
}
private fun showQuestion(textView: TextView, questions: Collection<TrueFalseQuestion>) {
val obj: TrueFalseQuestion = questions.elementAt(questionIndex)
textView.text = obj.question
storeAnswer(obj.answer)
val correct: Button
val wrong: Button

@ -25,16 +25,25 @@
android:text="@string/are_you_sure" />
<Button
android:id="@+id/btnCheat"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_margin="10dp"
android:backgroundTint="#888"
android:text="@string/show_answer"
android:textColor="@color/black" />
<Button
android:id="@+id/btnTrue"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_margin="10dp"
android:backgroundTint="#888"
android:text="@string/show_answer"
android:textColor="@color/black" />
<TextView
android:id="@+id/textViewAnswerCheat"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="@string/answer_goes_here" />
</LinearLayout>

@ -14,4 +14,5 @@
<string name="are_you_sure">Êtes-vous sûr?</string>
<string name="show_answer">Voir réponse</string>
<string name="cheat">Tricher</string>
<string name="answer_goes_here">la réponse va ici</string>
</resources>

@ -13,4 +13,5 @@
<string name="are_you_sure">Are you sure about that?</string>
<string name="show_answer">Show answer</string>
<string name="cheat">Cheat</string>
<string name="answer_goes_here">answer goes here</string>
</resources>
Loading…
Cancel
Save