Android - application logo activity

I have what seems like a simple question, but for me life cannot understand it ...

I have a main action that displays the company logo, in fact, a splash screen. I want this to appear for 2 seconds or so and disappear before the actual main action of the application. I tried to implement the use of sleep, but it gives me a blank screen for logo activity. It seems that the image is not loaded until AFTER sleep is completed. Essentially, the application launches, displays a black screen for 2 seconds, then proceeds to my application. If I click, I will see the logo. What am I doing wrong here? This is my logo code. Logo.xml has one ImageView with a resource:

public class Logo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logo); // Intent to jump to the next activity Intent intent= new Intent(this, NextActivity.class); this.startActivity(intent); SystemClock.sleep(2000); } } 
+4
source share
4 answers

You are blocking the UI thread, which is a big no-no. The system cannot draw the screen until the onCreate method onCreate . The usual way to do what you want is to start a separate thread that waits, and then sends Runnable to the UI thread:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logo); final Handler handler = new Handler(); final Runnable doNextActivity = new Runnable() { @Override public void run() { // Intent to jump to the next activity Intent intent= new Intent(this, NextActivity.class); startActivity(intent); finish(); // so the splash activity goes away } }; new Thread() { @Override public void run() { SystemClock.sleep(2000); handler.post(doNextActivity); } }.start(); } 

A slightly simpler way (as Athmos suggested in his answer) is to let the handler do the countdown for you:

 Handler mHandler; Runnable mNextActivityCallback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logo); mHandler = new Handler(); mNextActivityCallback = new Runnable() { @Override public void run() { // Intent to jump to the next activity Intent intent= new Intent(this, NextActivity.class); startActivity(intent); finish(); // so the splash activity goes away } }; mHandler.postDelayed(mNextActivityCallback, 2000L); } 

This has the advantage that you can undo the transition to the next action (if, say, use presses the back button or if you find an error condition or something during these 2 seconds):

 @Override protected void onPause() { if (isFinishing()) { mHandler.removeCallbacks(mNextActivityCallback); } } 
+8
source

Instead of using sleep in a constructor that delays the construction of the view for these 2 seconds, so it only shows the image AFTER 2 seconds, try the tip here

http://androidforums.com/40306-post4.html Also for the problem with the back button, add

 android:noHistory="true" 

for manifest for logo activity

0
source

using onResume () and a handler, a simple and clean way

You tried to put this ...

 // Intent to jump to the next activity Intent intent= new Intent(this, NextActivity.class); SystemClock.sleep(2000); //NOTE before the start of the next activity this.startActivity(intent); 

... in onResume ();

As far as I know, this is not an idea that the activity is displayed correctly when the onCreate () function is called. But onResume () is always called, and as far as I know, it should ever be displayed. If you want to show it only once, make your flag for it. see Activity Life Cycle

But in any case, what you are doing wrong, use Handler, Thread or similar for this

Other solutions:

  • Why not display ImageView Infront of everything and just hide it? after 2? you can just use the Handler for your delay. Or even use transparency to make a fantastic fade out. Take a look at ImageView setAlpha(...) .
  • Why not use Handler instead of sleep(...) . IMHO sleep(...) may cause "Application not responding"

this code should work:

 Handler mHandler = new Handler(); class MyTask implements Runnable{ public void run(){ //start next activity or fadeout ImageView or ... } } 

now called onResume:

 mHandler.postDelayed(new MyTask(), 100); //or any other Runnable 
0
source

You must use sleep in the new thread because it stops executing the oncreate method, and you can use it after setting setcontentview

0
source

All Articles