I am implementing a splashscreen for a android application. I only want to show the splashcreen once when the app is newly started. After i've done some work I want to go on to the app. If the user then presses the back button I don't want it to return to the splashscreen, I just want the app to exit. How can I implement this in the best way? How do I clear the backstack of the first activity.
If you want to show splash screen only for first time when app launches,
then you can use above solution of share preferences.
But i guess you want to go through following scenarios:
You start the app you get splash screen.
Then you navigate though the app.
Then you come to home screen of you app.
Then you want to exit but splash screen comes.
If you are having this problem then you need to finish splash screen
when you start home activity and also at the end you need to logout or finish the app home activity.
Also try android:launchMode="singleTask" in splash screen activity tab in android manifest.
Use shared preeferences in android and store the value 1st time.. from second time check if value present dont display splash screen
Edit for preferences follow this
When you are going to the second activity after the splash screen, call finish()
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (preferences.getBoolean("firstTime", true)) {
// Show splash screen
// Wait a few seconds.
} else {
// Nothing to do here. Go straight to the second activity.
}
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(
getSupportActivity()).edit();
editor.putBoolean("firstTime", false);
editor.commit();
startActivity(MainActivity.this, ...)
finish();
This way when the user presses back, there wont be any activity in the stack.
Hi, try this code it will help you but it will show the splash whenever you open the app.
public class spash_scr extends Activity {
ImageView t;
//LoginButton b;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spash_scr);
// bm1=drawable.shineme;
t = (ImageView) findViewById(R.id.textView1);
t.setImageResource(R.drawable.shineme);
RotateAnimation r = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// r.setStartOffset(1000);
r.setDuration(2000);
r.setFillAfter(true);
t.startAnimation(r);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(spash_scr.this, MainActivity.class);
startActivity(i);
finish();
}
}, 3000);
}
You weren't clear in the exact behavior, so here are some options:
A: You want it to show the splash activity every time the task (app) is restarted, such as after phone reboots, user's manual task closing, or when Android drops it for memory reasons. (This is usually used for branding or licensing logos.) In those cases, launch the splash from the main activity's onCreate(), then Finish() the splash screen to allow the user to return to the main view. That way navigating back won't bring the splash activity back, since it is not in the navigation stack anymore.
B: You want it to show the splash screen the first time the app is launched after installation, but never again. (Usually used for 'welcome' or 'get started' help views.) Use a SharedPreference setting as described in other answers here, or in the Using Shared Preferences documentation. In the case it should show the splash, I still suggest option A for the simplest way to not show the splash screen again for after it is first dismissed after the first launch.
C: An even more, unknown complex navigation? Learn about Tasks and Back Stack and you can make it do pretty much whatever you want.
After opening the new Activity this.finish (); You should do
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish ();
For that you should use SharedPreferences ,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
Intent i = new Intent(splash.this, otherSplash.class);
this.startActivity(i);
this.finish();
}
else
// Not firsttime Direct it as you wish
}
Related
In Android, I want to show Splash Screen only when the app is starting fresh and not when it is working on the background and resuming by the user.
So after reading here in Stackoverflow and over the web, I have added finish() before starting my home activity:
#Override
protected void onPostExecute(String s) {
finish();
Intent appStarterIntent = new Intent(StarterActivity.this, HomeActivity.class);
StarterActivity.this.startActivity(appStarterIntent);
}
But Still when I (returning) opening the app from the background I see the Splash screen...
How can I make the plash screen apprear only on fresh start of the app?
Save value 'isFreshStart' into SharedPreferences and start app only if there is no saved value. Remove this flag in onTerminate() method of Application class.
The point is, that Application instance gets destroyed when app is closed coimpletely, so you will know if it is a fresh start.
I'm building an app which has an Intro page with many slides. Once a first time user has gone through the intro, he'll be directed to a login screen. Once he logged in (or registered), he'll be taken into the app home page. As long as the user doesn't sign out, if he clicks on the app icon he'll be directly taken to the home screen.
I'm using the Intro page intent as the LAUNCHER activity and using sharedpreference to save 'first usage' and logged in states. By testing if the user has logged in or a first time user, I'm directing him to different intents.
So my question is, where is the most suitable position to have this intent redirection? Because Intro page has so many fragments and components, setting it as the LAUNCHER activity and having all the if else statements there to decide where the user should go, have I wasted system resources? Because if the user has already logged in, he'll taken into the home page without showing any app intro stuff which are loaded.
Or is it a good practice to create an empty activity and set that as the LAUNCHER activity and put all the if else statements in that. So the app doesn't need to go to the 'heavy' app intro page.
PS: I've declared those intent direction if else statements in the onCreate right after super.onCreate();
#Override
protected void onCreate(Bundle savedInstanceState) {
// Fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
// activity_first_usage is the container for all frames
setContentView(R.layout.activity_first_usage);
logger = new Logger(this);
if (!logger.isFirstUsage()) {
if (logger.hasTOKEN()) {
// If user didn't log out, then he can stay in the app
Intent home = new Intent(getApplicationContext(), Home.class);
startActivity(home);
finish();
} else {
// If this is not the first time user login in, no need to show the intro
Intent directToSignIn = new Intent(getApplicationContext(), SignIn.class);
startActivity(directToSignIn);
finish();
}
} else {
// If not, continue with the Intro and set usage status to used
logger.setFirstUsage(false);
}
...
}
ill tell you the concept
use a splash and there use a condition to check user's state eg: already registered , new one , registered but still did not go through intro like wise
identify it
now you use shared preff
can write a file
can keep a enum value
or get from a server etc..
more : you can think about what happens when user uninstall your app and re install it.Then what you need to do ? up to you.
once you identify the state of the user in the splash
the write different intents to each of them
if a new one - display your intro
if not - load to your main menu
you need to decide cuz you knows the requirement
Hope this helps a bit :)
how do i do it:
The first time each user visits the Apps screen, a slide show will appear instead of a splash screen which will tell the user about various features.
4 slides will come on the very first run of the app. slides will be changed using the swipe gestures
the slideshow will appear only for one time after the very first download.
Check this image as a reference: http://main.makeuseoflimited.netdna-cdn.com/wp-content/uploads/2012/03/HiLauncher6.png?323f2c
All you have to do is store a Boolean variable inside the app using SharedPreferences which will determine whether the slideshow has appeared once or not, if so the don't show it again.
For example:
SharedPreferences sp = getSharedPreferences(MY_PREFS_NAME,
Context.MODE_PRIVATE);
boolean isFirstTime = sp.getBoolean(IS_FIRST_TIME, true);
if (isFirstTime) {
showSlideshow()
} else {
// Go to your main activity
Intent main = new Intent(Start.this, MainActivity.class);
startActivity(main);
finish();
}
Don't forget to set isFirstTime to false once the slideshow has been shown, and store it in the sharedPreferences
Hope that helps
I want my app should be close on splash screen. Actually the whole scenario is from server side if app is blacklisted then app should not open complete, it open show splash screen and show dialog that "you are not authorized " then on click of ok button app should close.. It is not working.. on splash screen why? It is working on Main Activity only
I suggest you should use SharedPreferences and save the state like a boolean isAuthorized set it to true then in your SplashActivity do the following:
SharedPreferences prefs = getSharedPreferences("PREFS_NAME", 0);
boolean isAuthorized = prefs.getBoolean("isAuthorized", false);
if(isAuthorized)
{
startActivity(new Intent(Splash.this, MainActivity.class))
}
else
{
finish(); // or show a dialog that prompts user that you are not authorized....
}
If you have never used SharedPreferences follow this link.
Hope this will help you.
I have to exit from the app without clicking exit button.. I have 3 activities. One is main, another one is register and the other is login. I am in the login page so that if I press back in the mobile , I should go directly to the home.. Help me friends !! Thankyou..
Use the below code to finish current activity when moving to another activity this will ensure that no activity runs currently except current.
Intent intent=new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
CurrentActivity.this.finish();
So now when you click back button it will not display the T&C page and will exit. But if you want to show any other page instead of exit then override onBackPressed() method.
U want once u saved the details after that u want to open a new activity i hope .See this i have also done the same thing it will help u
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
if(shared.contains("username") && shared.contains("password")){
startingActivity();
} else {
//What u want to do here
}
I think the problem, as I understand, is that you are only calling finish() if the SharedPreference value is false. But you want to close this page no matter what or else it is still in the stack. So move finish() out of the if and close this Activity no matter what the value is.
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false);
if (!haveWeShownPreferences) {
Intent y=new Intent(this,Registerwithdialus.class);
this.startActivity(y);
}
finish();
Let me know if I don't understand the problem.