简体   繁体   中英

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.

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.

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.

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:

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

Intents take a little while to start up. 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.

But you did ask for a way to keep your toast in THIS class so here's one:

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. If you need data from your main class then you can send it as an extra in your intent.

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?

You should add the money value to the intent and show them when the PrivateWallet is displayed. The finish() does not finish the activity right away, so that why you see the toast before the other activity is started.

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