divya
divya

Reputation: 53

How to display welcome screen only one time in a day on android?

How to display welcome screen only one time in a day on android? I am preparing a screen with emojis rain fall and Iwant to display it only once in a day when the app is opened for the first time.

public class MainActivity extends AppCompatActivity {
    private EmojiRainLayout emojiRainLayout;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        emojiRainLayout = (EmojiRainLayout) findViewById(R.id.activity_main);
        button = findViewById(R.id.startbutton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emojiRainLayout.addEmoji(R.drawable.bell);
                emojiRainLayout.addEmoji(R.drawable.coconut);
                emojiRainLayout.addEmoji(R.drawable.deepam);
                emojiRainLayout.addEmoji(R.drawable.flower);
                emojiRainLayout.addEmoji(R.drawable.folded);

                emojiRainLayout.stopDropping();
                emojiRainLayout.setPer(4);
                emojiRainLayout.setDuration(7200);
                emojiRainLayout.setDropDuration(2400);
                emojiRainLayout.setDropFrequency(500);
                emojiRainLayout.startDropping();
            }
        });


    }
}

Upvotes: 3

Views: 709

Answers (3)

Sagar N
Sagar N

Reputation: 1

//In onCreate

public static final String MyPREFERENCES = "MyPrefs";
Calendar c = Calendar.getInstance();    //get current date.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    String currentDate = df.format(c.getTime());
    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    boolean showWelcome = sharedpreferences.getBoolean("isShowWelcomeScreen", false);
    if (showWelcome) {// normal flow
        String appOpenDate = sharedpreferences.getString("appOpenDate", "");
        boolean flag = compareDate(currentDate, appOpenDate);
        if (!flag) {
            //show welcome screen.
        }
    } else {
        //show welcome screen.
        //In Welcome screen activity/ fragment.
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString("appOpenDate", currentDate); //save date when app open
        editor.putBoolean("isShowWelcomeScreen", true);
        editor.apply();
    }

Upvotes: 0

Sagar Zala
Sagar Zala

Reputation: 5134

Solution:

SharedPreferences sharedPrefs = getSharedPreferences("MyPref", 0);
long time = sharedPrefs.getLong("displayedTime", 0);
if(time == 0 || time < System.currentTimeMillis() - 259200000) // 259200000 (Millisecond) = 24 Hours
{
    // Show welcome screen
    SharedPreferences.Editor prefsEditor = sharedPrefs.edit();
    prefsEditor.putLong("displayedTime", System.currentTimeMillis()).commit();
    prefsEditor.apply();
}

Upvotes: 0

aolphn
aolphn

Reputation: 2998

  1. When you first show it,save currentTime into SharedPreferences,like save System.currentTimeMillis() as t1,
  2. For second time ,get current time t2 by System.currentTimeMillis() ,
  3. Read your t1,and get day1 and day2 by new SimpleDateFormat("yyyy-MM-dd") from t1 and t2,
  4. Compare day1 and day2,if day2 bigger than day1 then show it and update your t1 in SharedPreference or day1 and day2 is same then do nothing.

Upvotes: 1

Related Questions