Passing Data Between Activities

Creating an activity typically involves touching at least three files: the Java class file, an XML layout,
and the application manifest.

Intent是一个重要概念

An intent is an object that a component can use to communicate with the OS. The only components
you have seen so far are activities, but there are also services, broadcast receivers, and content
providers.

Intent在这里的目的:

1) 用来启动activity;

2) 使用intent extras在创建intent时添加需传送的信息,类似于intent构造函数的参数;

Step1: 在接收方的Activity中创建一个newIntent方法【依赖倒置原则,甲方才知道自己需要什么数据】

private static final String EXTRA_ANSWER_IS_TRUE =
        "com.bignerdranch.android.geoquiz.answer_is_true";
public static Intent newIntent(Context packageContext, boolean answerIsTrue) {
    Intent intent = new Intent(packageContext, CheatActivity.class);
    intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return intent;
}

Step2: Launching CheatActivity with an extra(在发送方中创建新的activity)

boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
startActivity(intent);

Step3: 保存发送方发送的数据

private boolean mAnswerIsTrue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);
    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
}

Step4: 接收方应用发送来的数据

mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mShowAnswerButton = (Button) findViewById(R.id.show_answer_button);
mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mAnswerIsTrue) {
            mAnswerTextView.setText(R.string.true_button);
        } else {
            mAnswerTextView.setText(R.string.false_button);
        }
    }
});

Getting a result back from a child activity

Step1: 给子activity发送一个请求码以标识

startActivityForResult(intent, REQUEST_CODE_CHEAT); //给子activity发送一个请求码

Step2: 把子activity中用户操作返回给前一个activity

private void setAnswerShownResult(boolean isAnswerShown) {
    Intent data = new Intent();
    data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
    //调用Activity的setResult方法来返回数据给QuizActivity
    //其与QuizActivity的onActivityResult是呼应的。这个方法类似于“激发事件setResult(RESULT_OK, data);
}

Step3: 访问子activity发送来的数据

//这个方法类似于“事件相应函数”.此方法由ActivityManager来调用
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }
    if (requestCode == REQUEST_CODE_CHEAT) {
        if (data == null) {
            return;
        }
        mIsCheater = CheatActivity.wasAnswerShown(data);
    }
}

Step4: 应用返回的数据

private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
    int messageResId = 0;
    if (mIsCheater) {
        messageResId = R.string.judgment_toast;
    } else {
        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
    }
    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值