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?
- When you first show it,save currentTime into SharedPreferences,like save System.currentTimeMillis() as t1,
- For second time ,get current time t2 by System.currentTimeMillis() ,
- Read your t1,and get day1 and day2 by new SimpleDateFormat("yyyy-MM-dd") from t1 and t2,
- 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
How to Add a View Programmatically to Relativelayout
How to Find Memory Leaks from Native Code in Android
How to Set The App Icon as The Notification Icon in The Notification Drawer
How to Enable Cookies in Android Webview
Can't Create New Kotlin Project After Updating to Android Studio 4.2
How to Detect Android Listview Scrolling Stopped
Play Store Having New Version of The App, But Not Showing Update
Android - Open Target _Blank Links in Webview with External Browser
Evenly Spaced Menu Items on Toolbar
Android Serversocket Programming with Jcifs Streaming Files
Ionic Cordova App Stopped Compiling After Google's June 17Th Firebase Sdk Update
Android Alarmmanager Setexact() Is Not Exact
Handle Screen Orientation Changes When There Are Asynctasks Running
How to Click on an Item Inside a Recyclerview in Espresso
How to Set Margins for Textview Programmatically
View Pager + Imageview +Pinch Zoom + Rotation
How to Launch Android Calendar Application Using Intent (Froyo)