skip to Main Content

It was visible at first, but I don’t know if it’s related to the emulator, but my toast message is no longer visible. Can you help me?

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends AppCompatActivity {
    private static final String PREFS_NAME = "MyPrefsFile";
    private static final String KEY_FIRST_TIME = "first_time";

    private boolean isFirstTime() {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        boolean firstTime = preferences.getBoolean(KEY_FIRST_TIME, true);
        if (firstTime) {
            preferences.edit().putBoolean(KEY_FIRST_TIME, false).apply();
        }
        return firstTime;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (isFirstTime()) {
            Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
        }

        new Handler().postDelayed(() -> {
            Intent i = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(i);
            finish();
        }, 5000);



    }


}

my code is like this And as I said, the toast message appeared on the screen at first, but I don’t know if I did something, but it no longer appears. Could it have something to do with xml files?

2

Answers


  1. By default Toast messages are visible for few seconds.

    As you are using Toast.LENGTH_SHORT it will show for short time. If you want to have for a little longer you can use Toast.LENGTH_LONG.

    And for custom duration use setDuration() method of toast.

    Login or Signup to reply.
  2. It seems like an emulator issue. Your toast should be displayed in any case based on code you provided.

    Try to:

    1. Cold boot emulator
    2. Test on real device
    3. Delay toast showing

    Note: never use splash screen in this way. It’s designed to be something like loading screen, not with fixed timer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search