3

Inside onCreate of Application class, I set its instance to a static field, then show all application Toasts through this context. All works good except one thing, in some places a Toast can be shown before first activity can even appear but Toast never appears or sometimes just flashes. I think its because Activity not shown or drawn yet ? Or I'm missing something.

Edit: More like showing toast from onCreate of Application class

Edit 2 :

public class TestApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this, "Test from App", Toast.LENGTH_LONG).show();
    }
}
xmen
  • 1,947
  • 2
  • 25
  • 47
  • I was talking about `onCreate` of `Application`. – xmen Jan 11 '14 at 04:21
  • 2
    When Activity is started and application is not loaded, then both onCreate() methods will be called. But for subsequent starts of Activity, the onCreate() of application will not be called. – Linga Jan 11 '14 at 04:25

2 Answers2

0

If you want to show a Toast without the application even started manually by the user you can register a BroadcastReceiver which listens to the BOOT_COMPLETED system broadcast and then start a Service which will handle your Toasts.

You'll find many examples on how to do this.

Community
  • 1
  • 1
Endzeit
  • 4,810
  • 5
  • 29
  • 52
0

To make a Toast before drawing your layout resources, just do this following.

//put this code before your setContentView(R.layout.your_layout);

  Toast.makeText(yourclassName.this,"your text here",5000).show();

Well, consider one thing. If you want this toast to be shown before loading your activity and notify users some message. Then it might not be possible always. Because, The time Toast is shown with fraction of nano/mili seconds your layout is being loaded as-well.Moreover, scenario is totally different when you are on a real device and on a emulator.This might be the cause you got a flash of your Toast message. Just run it on a real device and you will see the differences.

Hope that helps

androCoder-BD
  • 498
  • 7
  • 13
  • 1
    I just want to show `Toast` as application start, no matter if something else shown or not. And `Toast` doesn't appear from `onCreate` of `Application`(not of `Activity`). And sometimes it just flashes. PS : Actually I test on real device first then on emulator. – xmen Jan 11 '14 at 14:44