Search code examples
androidoncreateandroid-toast

if i put a toast message in onCreate() function, will message be printed infinitely?


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listen();
    }

    public void listen() {
        Toast a = Toast.makeText(MainActivity.this,"HI",Toast.LENGTH_SHORT);
        a.show();
    }
}

Will this goes on printing the HI string..?


Solution

  • No It won't. However, onCreate() function is called somewhat more often than you think! (like on Screen Rotation and more ...)

    Checkout Activity Lifecycle and learn when onCreate() is called.