简体   繁体   中英

Pass data from activity A to activity B

Here's what I want to do: I have a quiz where the user has to answer three questions. After the three questions, there is a result screen where the user sees what he has answered.

So I want to get the answer from activity A, activity B and activity C to the result activity. The only problem is that every example or tutorial on the internet is going from activity A to activity B and then back to activity A. But I don't want to get back, I want to go forward. Is this even possible?

In my question one activity (activity A) I have this code. The intent data is send to the second question instead of the result screen as mentioned above, purely to test if the onActivityResult() works. The code below is inside an if/else statement, to check for the correct answer.

QuestionOneActivity

Intent i = new Intent(QuestionOne.this, QuestionTwo.class);
    i.putExtra("result", buttonText);
        setResult(RESULT_OK, i);
    startActivityForResult(i, CONTROLE_CODE);

Log.e("Send", "Send");

The buttonText variable is the answer that the user chooses. I already checked if this variable is null, and it isn't. The CONTROLE_CODE variable is set to 0;

And in my QuestionTwoActivity (activity B), there is this code to receive the data from

QuestionTwoActivity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if ((resultCode == RESULT_OK) && (requestCode == CONTROLE_CODE)) {
            retrieveData = data.getStringExtra("result");
            Log.e("Data", "Data: " + retrieveData);
        }

        Log.e("The answer is:", "The answer is" + retrieveData);
    }

The retrieveData is a String variable and is declared at the top of my Activity.

The Log.e("Send", "Send"); from the QuestionOneActivity is visible in my LogCat. But the Log.e("Data", "Data: " + retrieveData); or Log.e("The answer is:", "The answer is" + retrieveData); from the QuestionTwoActivity isn't shown in my LogCat. So it doesn't get invoked.

Maybe this is a duplicate question, but I didn't find the answer for my problem. So what is the correct way to send data from Activity A, Activity B and Activity C to a result screen (Activity D)?

Thanks in advance.

Activity QuestionOne :

Intent i = new Intent(QuestionOne.this, QuestionTwo.class);
i.putExtra("result", buttonText);
startActivity(i);

Activity QuestionTwo :

Intent intent = getIntent();
String text = intent.getStringExtra("result");

in Question 1

Intent i = new Intent(QuestionOne.this, QuestionTwo.class);
i.putExtra("answer1", buttonText);
startActivity(i);

in oncreate function of question2 get previous answer

String Answer1 =  getIntent().getStringExtra("answer1");
Intent i = new Intent(QuestionTwo.this, QuestionThree.class);
i.putExtra("answer1", Answer1 );
i.putExtra("answer2", buttonText);
startActivity(i);

and so on for question three and then result

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