Send and receive extras with instance

main
Alexis Drai 3 years ago
parent 25806712b3
commit af21a58dee

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

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

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

@ -25,9 +25,8 @@
android:text="@string/are_you_sure" /> android:text="@string/are_you_sure" />
<Button <Button
android:id="@+id/btnTrue" android:id="@+id/btnCheat"
style="?android:attr/buttonBarButtonStyle" style="?android:attr/buttonBarButtonStyle"
android:layout_width="125dp" android:layout_width="125dp"
android:layout_height="75dp" android:layout_height="75dp"
@ -36,6 +35,16 @@
android:text="@string/show_answer" android:text="@string/show_answer"
android:textColor="@color/black" /> 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> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

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

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