简体   繁体   中英

How to load themed activity after correct or wrong answer in a quiz?

OK, I was adviced to make a new thread for my question. I am using imported SQLite database. Anyway, I have made two themed dialogs for right and wrong answers, and I want to show them when right or wrong answer is clicked. Here's the code in my activity, cut out parts:

private class Answer {
        public Answer(String opt, boolean correct) {
            option = opt;
            isCorrect = correct;
        }

        String option;
        boolean isCorrect;
    }

    final OnClickListener clickListener = new OnClickListener() {

        public void onClick(View v) {
            Answer ans = (Answer) v.getTag();
            if (ans.isCorrect) {
                Toast toastT = Toast.makeText(Kviz.this, "Correct!", Toast.LENGTH_SHORT);
                toastT.show();
                finish();
            }else{
                Toast toastP = Toast.makeText(Kviz.this, "Wrong!", Toast.LENGTH_SHORT);
                toastP.show();
                }
                startActivity(getIntent());
            }
    };

When I use Toast like above it works fine, but when I try to use my themed activity like this, it doesn't, instead it goes right to next question, and when I press back button on my phone I get that themed popup activity:

final OnClickListener clickListener = new OnClickListener() {

        public void onClick(View v) {
            Answer ans = (Answer) v.getTag();
            if (ans.isCorrect) {
                Intent t = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
                startActivity(t);
                finish();
            }else{
                    Intent p = new Intent("rs.androidaplikacijekvizopstekulture.POGRESANODGOVOR");
                    startActivity(p);
                }
                startActivity(getIntent());
            }
    };

To repeat, I get the themed activity when I press the back button, I get the wrong ones and the correct ones, as I go backwards. I just want to show that dialog and then to load the next question, like it does with the Toast. Just to mention that this activity loading works fine in my other acitivity, only it goes after simple button click, no question and answers.

The reason is after calling any of (right/wrong) startActivity , it moves on to call startActivity(getIntent()); method. One way is to make your TACANODGOVOR & POGRESANODGOVOR Dialog activity by adding following tag/value for these activities in manifest file android:theme="@android:style/Theme.Dialog" eg

<activity ... android:theme="@android:style/Theme.Dialog" />

It will display TACANODGOVOR & POGRESANODGOVOR as dialog and unless user presses back button (or any exit button activities may have), it wont proceed to next line.

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