-1

I made a sort of index activity with my personal logo. I want that after 5 seconds change activity. So I prepared 2 layout, I run the launcher. In launcher activity I set:

Intent intent = new Intent(Home.this, IndexActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        startActivity(intent);
        finish();

The problem is that the activity stay blocked in a white activity and after 5 seconds change activity.

Did I make a mistake?

How can I create a fade animation? And how can I run it? Thanks

pippo15
  • 113
  • 1
  • 4
  • 17

3 Answers3

2

You should never block the main thread. It is responsible for updating the UI and if you block it, the user interface will freeze.

Instead, use a Handler and its postDelayed method.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

Thread.sleep() degrades the performance. You can use the Handler class' postDelayed() method to perform this:

Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
        Intent intent = new Intent(Home.this, IndexActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
        finish();
    }

}, 5000L);
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • "Thread.sleep() degrades the performance" -> Not really, it does what it is supposed to. – 2Dee Mar 13 '15 at 16:51
  • @2Dee nah, it's not an efficient way to make any thread wait. Handler is much more better. – Apurva Mar 13 '15 at 16:53
  • can you explain why you think Thread.sleep() is "not an efficient way to make any thread wait" ? I could not find anything to support that statement ... – 2Dee Mar 13 '15 at 20:19
  • 1
    @2Dee I'm also looking for a link I read it on to post you. 2 or 3 well experienced guys already told me on SO not to use `Thread.sleep()` one of them posted me a link too. I'll tell you as I find it. – Apurva Mar 13 '15 at 20:23
  • Cool, thanks, I'll be looking forward to it. Thread.sleep() is obviously a bad idea on the UI thread, but other than that, I'm not aware of any performance drop if used properly on a separate Thread. – 2Dee Mar 13 '15 at 20:28
  • @2Dee but AFAIR using thread.sleep is better only to suspend a thread, if you want to put a thread a on hold is't not good to use thread.sleep – Apurva Mar 16 '15 at 09:43
  • I'm not sure I understand the difference you make between "put a thread on hold" and "suspend a thread", can you elaborate ? – 2Dee Mar 16 '15 at 09:45
  • @2Dee hey I don't run away. I just couldn't find answer, but now I have something to show you. [See the CommonsWare's answer to me in comment on this question](http://stackoverflow.com/questions/29078627/let-scheduledthreadpoolexecutor-execute-tasks-on-ui-thread?noredirect=1#comment46390883_29078627) and before you *argue* to him better know who is he first!!! **I hope a thirsty(since days) fellow is satisfied now.** – Apurva Mar 16 '15 at 14:56
  • @2Dee and if you want to me to elaborate `"put a thread on hold" and "suspend a thread"` then listen, a thread put on hold can be resumed, and suspending a thread means destroying it. – Apurva Mar 16 '15 at 14:59
  • 1
    Yup, that's what I thought, you didn't understand what you were doing. Commonsware answer basically says that it is more efficient to use postDelayed than creating a backgroung thread if you need to do background operations AND get UI work done as well. He never implies that Thread.sleep causes performance issues. And your definition of Thread.suspend is also incorrect, see http://www.tutorialspoint.com/java/java_thread_control.htm or http://developer.android.com/reference/java/lang/Thread.html#resume(). You're just embarassing yourself here... – 2Dee Mar 16 '15 at 15:03
0

First declare this in your animation folder.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

Then declare an animation in acitivity's java file from where you are going :

    Animation animfadein,animFadeout;
     animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.fade_in);  
animFadeout = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.fade_out);  

Then after startActivity(Intent);

write overridePendingTransition(animFadein, animFadeout);

here'sfadeout :

fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>