android splash screen - android

i have a program that shows a splash screen.But the problem is whenever i refresh same page again it appears.Is there any method to stop splash screen again and again.I want it just comes at first time not again and again.
Thank you

So you basically want the splash screen appear once per app launch.
Here's a quick and dirty way:
Subclass android.app.Application as, say, MyApp;
Declare that class in AndroidManifest.xml (<application android:name=".MyApp" ... >) so that it would get instantiated at app launch time;
Give it a public static boolean SPLASH_SHOWN = false;
Now, in your Activity's onCreate() check if SPLASH_SHOWN = false, show splash and set it to true.

If you implement the splash screen in the same activity with another view layout (the main layout) - for example, display splash screen first, then swich the view to the main layout - I suggest to control the content view setting in onResume().
Use a boolean variable (displayedSplash) to remember whether the splash screen has been displayed. If it has not been displayed (start the activity the first time, displayedSplash == false), then set the view to the splash screen, after that switch to the main layout and set displayedSplash = true. If it has been displayed (refresh the page, displayedSplash == true), just set the view to the main layout.
Another simple and safe solution for simple splash screen is implementing it as an activity, then start the main activity. When the onResume() is called the second time, just finish the activity. Please refer to this Wiki for the detail.

Most of the times I call finish() method inside onPause() method in splash activity. This will work fine for me

Related

Android shared element transition from splash screen to main activity

My application displays a splash screen for 1 second before displaying the main activity. Both the splash screen and main activity share a common image that is required to animate from the center of the splash screen to the top of the main activity layout.
Since it wasn't obvious how to accomplish this animation if the splash screen was implemented as a <layer-list> background image in the main activity (see Splash Screens the Right Way or How do I make a splash screen?), I decided to implement the splash screen as a normal activity and use a shared element transition to animate the image between the two activities. Initially, I used the following onCreate() implementation in the splash activity:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
String transitionName = ViewCompat.getTransitionName(imageView);
Intent intent = new Intent(this, LoginActivity.class);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(
this, imageView, transitionName);
imageView.postDelayed(() -> {
ActivityCompat.startActivity(SplashActivity.this, intent, options.toBundle());
finish();
}, 1000);
}
}
There are two problems with this approach:
Calling finish() immediately after calling startActivity() causes the splash activity window to be hidden/destroyed before the animation starts which results in the home screen temporarily flashing into view during the animation.
Pressing back from the main activity automatically triggers a shared element return transition that results in the image appearing suspended over the home screen for 500 ms after the main activity window has been closed. The return transition fails because the splash activity has already called finish() and therefore is no longer on the back stack.
To solve the first problem, I wrapped the finish() call in a postDelay() Runnable to ensure that it only gets invoked once the shared element transition has completed. A 1500 ms delay works in my application, but that value should be adjusted to depending on the timing required by other use cases.
...
imageView.postDelayed(() -> {
ActivityCompat.startActivity(SplashActivity.this, intent, options.toBundle());
imageView.postDelayed(this::finish, 1500);
}, 1000);
To solve the second problem, I overrode the main activity's onBackPressed() method to directly call finish() thereby avoiding the default implementation's call to finishAfterTransitions(). This prevents the Activity from attempting to perform the shared element return transition.
#Override
public void onBackPressed() {
finish();
}
Any alternative approaches or suggestions to improve this solution would be appreciated.
Why don't you use fragments that sharing same activity instead? I don't see a reason to use 2 different activities for such a simple things.
Instead of finish(); use ActivityCompat. finishAfterTransition(this);
I faced similar problem also.
One approach can be to not to call finish() in Splash screen and in next screen call finishAffinity() on onBackPressed().

Android - how do I show a startup screen to cover initial processing?

I have an Android app that does about 30 seconds of heavy duty number crunching at the start of the app. How do I display a screen that disappears at the end of the initial processing?
What I currently do is:
My activity_main.xml's topmost view is a ViewAnimator which has (for now) two child views - the main view, and a FrameLayout which currently is a white background that fills the screen and a child text view with "Processing..." on it.
My MainActivity.java's onCreate() method calls a user-defined method doInitialProcessing(); doInitialProcessing calls the ViewAnimator's setDisplayedChild method to show the "Processing" view, then does the number crunching, then calls setDisplayedChild again to show the main view.
In the Eclipse Emulator, what usually happens is, the screen turns white at first, then goes black, then the main view appears. (Every now and then, the "Processing..." text shows up.)
Is this the right way to go about this? Show the initial call to setDisplayedChild be in a separate thread? Does the fact that it is called from onCreate have anything to do with it?
There are two ways to do it:
Create a normal activity with a layout for your splash and start the other activity using a timer. (this way, you cannot load data in the second activity)
The other way is to create a splash fragment, that is displayed for a few seconds while the content is loaded in the background.
I use the following timer to start my activity from SPlash activity. This can also be used in a fragment:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent in = new Intent(SplashActivity.this, HomeActivity.class);
startActivity(in);
finish();
}
}, SPLASH_TIME_OUT);
SPLASH_TIME_OUT is the delay in integer value. (1000 for sec)
You can use a Splash Screen that shows something like the logo of your app for instance for some seconds while the app munches on the numbers.Here is a good tutorial on creating Splash Screens.You can also read up on this StackOverflow question.

Android-Intent navigation to skip an intent/activity

Here is my scenario, on load of my main activity I have a checking that looks like this.
activity onCreate:
if(true) {
navigate to other intent
}
setContentView(layout);
//..rest of the code.
You see. If my condition is met, I want to navigate to other intent. Otherwise, I load the content view of the current activity. However, during implementation, an empty content view shows first before the "other intent".
Do you have any recommendation on how I can immediately load to the "other intent"? Thanks.
#Techfist, thanks I've set my default theme to transparent. so, my previous condition was met. However, in this scenario:
if(false) {
navigate to other intent
}
setContentView(layout);
//..rest of the code.
my activity was shrunk. so I added this.
Change Activity's theme programmatically
Try this:
Set the default theme of your first activity to transparent,
In this way when you condition is met, you will see backgound as transparent mimicking scenario you want, but remeber still activty was launched so it will remain on stack, hence if you press back from second activity first will be popped up disturbing your scenario again, so to tackle this, when you launch second activity upon condition dont forget to finish it, hence when you press back from second it wont return to first and directly exit.
Is condition is not met simply proceed by setting up content view
Hope it help, if you face probelm again then try starting second activity for result, and when second finishes, finist first as well on receive result.

android - How to Exit from the application

My Application have 20 activities. Here i want to implement the how to exit from the application when you click on the button(like Logout). it means if you click on the menu button any where of our application then it shows the one button. if click on that then directly comes out from the application. how to implement it.
thanks
Well naresh you can do something like that
first finish the activity from which you are closing application this.finish(); secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this clear top when you switch from one activity to another activity and as you know every activity is kept in stack to so this flag remove old activity from top and push new activity in top so around your whole application only one activity is kept in stack
and if this does not work put the whole application in background by avtivityname.moveTaskToBack(); this will move your whole app in back ground but only one drawback when you start your activity it will show your that activity from which you have moved back
System.exit(0);
should work, don't forget Java common functions work on android, there isn't only the android library!
As for the button being in the menu in every activity, you could create a class derived from Activity which creates and handles the menu properly, and make every other activity inherit that derived activity.
First finish the activity from which you are closing application: this.finish();. Secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); This clears top when you switch from one activity to another activity. As you know every activity is kept in a stack to so this flag removes old activity from the top and pushes new activity to the top so around your whole application only one activity is kept in the stack.
If this does not work, put the whole application in background with avtivityname.moveTaskToBack();. This will move your whole app to the background. One drawback: when you start your activity it will show your that activity from which you have moved back.

Android: Does onCreate method run in tabView when we click on each tab?

In my project I have three tabs. I want to know, each time that I click a tab onCreate function runs or it runs just one time when it creates.
Actually, in one tab I want to show a picture. for the first time, default picture is shown. However, if user creates his picture (in other tabs), when he clicks this tab the new picture should be shown.
My problem is, only default picture is shown and it doesn't show new picture. I think onCreate method don't run each time that I click the tab. Am I right?
hi friend it depends up on whether the activity is still active or not, if the activity is in paused mode then only the onResume() method will be called, if the activity is destroyed then the onCreate() method will be called,
my suggestions is that if u want to write any code u can use onResume()
or else
u can explicitly kill previous activities using finish method of activity. to make sure that every time onCreate() method is invoked..
u can call this finish() in the tab class u have written for tabs before switching to other tabs
I had the same problem. Copied the same code in onResume() method & it worked as expected.

Categories

Resources