Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android toast won't appear

Tags:

android

toast

It's getting really annoying. I'm trying to show a simple toast and it will just not appear.

public class MainActivity extends Activity implements UserPromptDialogListener  {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public ProgressDialog progressDialog;
    public static String SELECTION_TYPE="type";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CharSequence text = "I am a toast sss";
        Toast t1 = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
        t1.show();

        Log.d("AAA", "After toast show");
    }
    //other methods
}

I have tried this, MainActivity.this, etc. but it does not seem to work.

Any help is appreciated.Thanks.

like image 941
Margi Avatar asked Nov 03 '22 23:11

Margi


1 Answers

I just tested and this code and works fine in onCreate, I see the Toast right as the Activity is created.

One way to trigger onCreate:

  1. Close your app completely by pressing back until you reach the home screen.
  2. Reopen your app using the launcher icon or recent apps (press recent button on 4.0+ or hold home button).
  3. Check to see if there is a Toast.

Another way to trigger onCreate:

  1. Rotate the device, the Activity will be destroyed and recreated.

This will ensure your Activity goes through onCreate and you see the Toast. Tell me if either of those are working for you (they are for me).

like image 143
Steven Byle Avatar answered Nov 08 '22 08:11

Steven Byle