首页 > 解决方案 > 活动可见时立即显示 toast

问题描述

每当用户可以看到活动但消息根本不显示时,我都会尝试显示敬酒。

我有活动 X 在某些时候调用:

    Intent t = new Intent(this, MainActivity.class);
    t.putExtra( "toast", getString(R.string.subscribingInBackground));
    t.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(t);
    finish();

在 MainActivity 除了我还有很多其他的东西:

@Override
    public void onResume() {
        super.onResume();
        if (this.adView != null) {
            this.adView.resume();
        }


            String toast = getIntent().getStringExtra("toast");
            if (toast != null)
                Toast.makeText(this, toast, Toast.LENGTH_LONG).show();

    }

toast始终为空

我究竟做错了什么?

标签: androidandroid-intentactivity-lifecycleandroid-toast

解决方案


FLAG_REORDER_TO_FRONT建议您相信该活动已经存在。如果是这样,该活动将被调用onNewIntent(),并且Intent交付给onNewIntent()将有你的额外。getIntent()返回Intent最初用于创建活动的 。


推荐阅读