简体   繁体   English

在开始一个新的 Intent 活动后使用 Toast

[英]Using Toast after starting a new Intent activity

I would like to view some text in Toast after a new Intent activity is being used.在使用新的 Intent 活动后,我想在 Toast 中查看一些文本。

This is what I have:这就是我所拥有的:

Intent i = new Intent(this, PrivateWallet.class);
startActivity(i);
this.finish();
Toast.makeText(getApplicationContext(), "You have € " + inputTransacVal.getText().toString() + " less.",Toast.LENGTH_SHORT).show();

The problem is, that Toast is showing some text first, before intent is starting.问题是,Toast 在意图开始之前首先显示一些文本。

Any help would be much appreciated.任何帮助将非常感激。 :) :)

Edit:编辑:

Here is the working code:这是工作代码:

Bundle bundle = new Bundle(); bundle.putString("value", "You have € " + inputTransacVal.getText().toString() + " more."); 
startActivity(new Intent(this, PrivateWallet.class).putExtras(bundle)); 
this.finish();

This code is meant to validate:此代码旨在验证:

if(this.getIntent().getExtras() != null){
    Toast.makeText(this, this.getIntent().getExtras().getString("value"),Toast.LENGTH_LONG).show();
}

You should definitively put the Toast code in the onCreate of your PrivateWallet activity.您应该明确地将 Toast 代码放在 PrivateWallet 活动的 onCreate 中。

The amount of Euro should be passed to your activity with a Bundle:欧元金额应通过捆绑传递给您的活动:

Bundle bundle = new Bundle();
bundle.putString("value", inputTransacVal.getText());
startActivity(new Intent(this, PrivateWallet.class).putExtras(bundle));

and then in your PrivateWallet Activity:然后在您的 PrivateWallet 活动中:

Toast.makeText(this, "You have € "+this.getIntent().getExtras().getString("value")+" less",Toast.LENGTH_LONG).show();

Intents take a little while to start up. Intent 需要一点时间才能启动。 Your toast executes more or less instantly.您的吐司或多或少会立即执行。 Therefore your toast should really be in your PrivateWallet class so that it will execute after the Activity has started up.因此,您的 toast 确实应该在您的 PrivateWallet class 中,以便在 Activity 启动后执行。

But you did ask for a way to keep your toast in THIS class so here's one:但是你确实要求一种方法来保持你的吐司在这个 class 所以这里是一个:

Hackish solution (could give you problems with race conditions... still...) It is what you asked for - put a delay on your toast using a timer...骇人听闻的解决方案(可能会给您带来比赛条件的问题......仍然......)这是你所要求的 - 使用计时器延迟你的吐司......

public void startNewActivity(){
    Intent i = new Intent(SplashScreen.this, YOURINTENTHERE);
    startActivity(i);        

    Timer t = new Timer();
    t.schedule(new SplashDone(), 1500);
}

public class SplashDone extends TimerTask {

    @Override
    public void run() {
                 //TOAST GOES HERE
    }
}

A better approach might be to do your toast in the PrivateWallet class.更好的方法可能是在 PrivateWallet class 中祝酒。 If you need data from your main class then you can send it as an extra in your intent.如果您需要来自主要 class 的数据,那么您可以将其作为您的意图中的额外数据发送。

Intent i = new Intent(getApplicationContext(), YOURACTIVITY);

Bundle bundle = new Bundle();
bundle.putString("value", inputTransacVal.getText());

i.putExtras(bundle);

startActivity(i);

Why don't you just send the inputTranscacVal.getText() to your PrivateWallet activity, and have that display the Toast on start up?为什么不直接将inputTranscacVal.getText()发送到您的 PrivateWallet 活动,并在启动时显示 Toast?

You should add the money value to the intent and show them when the PrivateWallet is displayed.您应该将货币价值添加到意图中,并在显示 PrivateWallet 时显示它们。 The finish() does not finish the activity right away, so that why you see the toast before the other activity is started. finish() 不会立即完成活动,这就是为什么您在其他活动开始之前看到 toast 的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM