tags:

views:

152

answers:

2

I'm trying to make my app to play intro clip for only when I start activities. But from my code it's always play the clip after wakeup before resume to app although I did not closed the app. What can I do to fix this prob? Thanks

From main:

startActivity(new Intent(this, MyIntro.class));

From MyIntro:

public class MyIntro extends Activity implements OnCompletionListener {

    int a;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.intro);
        playIntro();
    }

    public void onConfigurationChanged(Configuration newConfig) {
        setContentView(R.layout.intro);
    }

    public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub
        this.finish();
    }

    private void playIntro(){
        setContentView(R.layout.intro);

        VideoView video = (VideoView) this.findViewById(R.id.VideoView01);

        Uri uri = Uri.parse("android.resource://real.app/" + R.raw.intro);
        video.setVideoURI(uri);
        video.requestFocus();
        video.setOnCompletionListener(this);

        video.start();  
    }

}
A: 

What function have you overridden in your main Activity - the one where you call

startActivity(new Intent(this, MyIntro.class))

?

I would assume it's onResume() and the line above is executed too many times, because of that. Read again the explanation of Activity lifecycle here, it's the first thing I do, when I have problems like that.

Get back to us with a little more info about the main Activity.

Dimitar Dimitrov
A: 

Evil hack:

Add a static pointer to your own activity, fill or override it when "onCreate" gets called. If it's null, play your movie, otherwise, don't. You could do the same with a static boolean really.

private static boolean isRunning = false;

protected void onCreate(Bundle bundle) {
    super.onCreate(bundle)
    if(!isRunning)
    {
        isRunning = true;
        //Play your video here
    }
}

There are much more elegant and correct ways of doing this, but if you're in a hurry this will probably work.

haseman