How to Display a One Time Welcome Screen

How to display a one time welcome screen?

Save a flag in the Preferences when you start up the application, after you've done the welcome screen stuff. Check for this flag before you show the welcome screen. If the flag is present (in other words, if it's not the first time), don't show it.

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

  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.

Flutter One time Intro Screen?

If you wish to show the intro screen only for the first time, you will need to save locally that this user has already seen intro.

For such thing you may use Shared Preference. There is a flutter package for Shared Preference which you can use

EDITED:

Please refer to the below complete tested code to understand how to use it:

import 'dart:async';

import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
color: Colors.blue,
home: new Splash(),
);
}
}

class Splash extends StatefulWidget {
@override
SplashState createState() => new SplashState();
}

class SplashState extends State<Splash> with AfterLayoutMixin<Splash> {
Future checkFirstSeen() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool _seen = (prefs.getBool('seen') ?? false);

if (_seen) {
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (context) => new Home()));
} else {
await prefs.setBool('seen', true);
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (context) => new IntroScreen()));
}
}

@override
void afterFirstLayout(BuildContext context) => checkFirstSeen();

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text('Loading...'),
),
);
}
}

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Hello'),
),
body: new Center(
child: new Text('This is the second page'),
),
);
}
}

class IntroScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('IntroScreen'),
),
body: new Center(
child: new Text('This is the IntroScreen'),
),
);
}
}

Thanks to Ben B for noticing the incorrect use of delay in initState. I had used a delay because sometimes the context is not ready immediately inside initState.

So now I have replaced that with afterFirstLayout which is ready with the context. You will need to install the package after_layout.

How to run an activity only once like Splash screen

So here's what I did, in my SplashActivity(onCreate):

    SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", true);
editor.commit();

Intent intent = new Intent(this, RegistrationActivity.class);
startActivity(intent);

SplashActivity(onResume):

@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (!firstRun) {
Intent intent = new Intent(this, RegistrationActivity.class);
startActivity(intent);
Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
} else {
Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
}
}

In my RegistrationActivity(onCreate):

    SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();

boolean firstRun = settings.getBoolean("firstRun", true);
Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());

And then disabled back button to prevent going back unless the user presses Home:

@Override
public void onBackPressed() {
}

Big thanks for those that contributed!

Android:Splash screen only for the first time

Okey looking at your problem you can do following..

First of all declare object of SharedPreference and on String which will we use later.

SharedPreferences loginPreference;
String MY_PREF = "my_pref";

Now in onCreate of your SplashActivity, do something like this.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// initialize SharePreference
loginPreference = getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);

// this condition will do the trick.
if(loginPreference.getString("tag", "notok").equals("notok")){

// add tag in SharedPreference here..
Editor edit = loginPreference.edit();
edit.putString("tag", "ok");
edit.commit();

// your logic of splash will go here.
setContentView(R.layout.splash);

}else if(loginPreference.getString("tag", null).equals("ok")){
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}

Happy Coding..

How to display welcome screen after the installation of the app in Android

Try it, in your onCreate() method, add below code after your if condition

    if (!isFirstTimeStartApp()) {
startMainActivity();
finish();
}else{
setFirstTimeStartStatus(false);
}

In your setFirstTimeStartStatus() method,change the code to

 private void setFirstTimeStartStatus(boolean stt) {
SharedPreferences ref = getApplicationContext().getSharedPreferences("IndroSliderApp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = ref.edit();
editor.putBoolean("Flag", stt);
editor.commit();
}


Related Topics



Leave a reply



Submit