开发者

Using Toast after starting a new Intent activity

I would like to view some text in Toas开发者_如何学编程t 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();


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜