Wednesday, December 4, 2013

Start main activity after splash screen with animation

Modify from last exercise, in this post, we will start another activity (MainActivity) after animation of AnimationDrawable.

Start MainActivity after splash screen with animation


To start another activity, simple call startActivity() with intent.
   Intent intent = new Intent(
AndroidAnimationActivity.this, MainActivity.class);
startActivity(intent);


Create a new MainActivity.java, it's the new activity to run after splash screen. Simple display anything now.
package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView image = new ImageView(this);
image.setImageDrawable(
getResources()
.getDrawable(R.drawable.ic_launcher));
setContentView(image);
}

}


Modify run() method of MyTimerTask class in our splach screen activity, AndroidAnimationActivity.java in our example:
package com.exercise.AndroidAnimation;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {

AnimationDrawable myAnimationDrawable;

Timer timer;
MyTimerTask myTimerTask;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
myAnimationDrawable
= (AnimationDrawable)myAnimation.getDrawable();

myAnimation.post(
new Runnable(){

@Override
public void run() {
myAnimationDrawable.start();
}
});

//Calculate the total duration
int duration = 0;
for(int i = 0; i < myAnimationDrawable.getNumberOfFrames(); i++){
duration += myAnimationDrawable.getDuration(i);
}

timer = new Timer();
myTimerTask = new MyTimerTask();
timer.schedule(myTimerTask, duration);
}

class MyTimerTask extends TimerTask {

@Override
public void run() {

timer.cancel();
/*
runOnUiThread(new Runnable(){
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Animation Stopped",
Toast.LENGTH_SHORT).show();
}});
*/
Intent intent = new Intent(
AndroidAnimationActivity.this, MainActivity.class);
startActivity(intent);
}
}
}


Modify AndroidManifest.xml to add <activity> of ".MainActivity".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidAnimation"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidAnimationActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
</application>

</manifest>


From the demo video, it can be noted that:
- If the devie's configuration change (such as orientation) in MainActivity, it will still in MainActivity. Will not re-start the first activity.
- May be you have to special handle the BACK button depends on what you want, because of the first activity still in history stack in this simple implementation.

download filesDownload the files.

No comments:

Post a Comment