四个活动不显示Toast

时间:2017-04-03 13:41:52

标签: java android android-intent

我有四项活动:

活动A

 private void addCard() {
        Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
        startActivityForResult(intent, REQUEST_CODE_CREATE);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_CREATE) {
        if (resultCode == RESULT_OK) {
            if (data.hasExtra("data")) {
                // Card has been create
                Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

活动B

 Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
                intent.putExtra(EXTRA_MESSAGE, uunitValue);
                startActivityForResult(intent, REQUEST_CODE);

然后在第二个活动中,我必须将数据传递给第三个活动。

活动C

    Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
                                    Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
                                    intent.putExtra("card", card);
                                    startActivity(intent);
finish();

活动D

Intent data = new Intent(Stage3Activity.this,MainActivity.class);
                data.putExtra("data", card);
                startActivityForResult(data, RESULT_OK);

当我按下活动A上的按钮时,不会显示Toast。

3 个答案:

答案 0 :(得分:4)

您需要按照以下更新代码

if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CODE_CREATE) {
            if (data.hasExtra("data")) {
                // Card has been create
                Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
            }
        }
    }

首先检查RESULT_OK,然后继续

快乐的编码!

答案 1 :(得分:1)

getApplication()替换为this(当前活动的上下文)

Toast.makeText(this.class, "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();

答案 2 :(得分:0)

getApplicationContext()方法

中使用getApplication()代替makeText()