2

I have the following code:

public class SplashScreenActivity extends Activity {

private boolean animated ;

private Handler handler1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    if (!isPreviouslyLoggedIn()) {
        setContentView(R.layout.splash);

        final TextView revolution=(TextView) findViewById(R.id.textView1);
        final Button login=(Button) findViewById(R.id.loginButton);
        final Button signUp=(Button) findViewById(R.id.signUpButton);

        login.setOnClickListener(loginListener);
        signUp.setOnClickListener(signUpListener);

        if (!animated) {
            animated = true;
            revolution.setVisibility(View.INVISIBLE);
            login.setVisibility(View.INVISIBLE);
            signUp.setVisibility(View.INVISIBLE);

            ImageView image = (ImageView) findViewById(R.id.image);
            TranslateAnimation slide = new TranslateAnimation(0, 0, 100, 0);   
            slide.setDuration(1000);
            image.startAnimation(slide);

            handler1 = new Handler();

            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    revolution.setVisibility(View.VISIBLE);
                    login.setVisibility(View.VISIBLE);
                    signUp.setVisibility(View.VISIBLE);
                }
            },1200);
        }
    }

    else {
        setContentView(R.layout.home);
        Intent intent = new Intent(getApplicationContext(), PickUpActivity.class);
        startActivity(intent);
    }

}

When the user clicks on one of the buttons, it leads him to a different activity in the same app. However, when the user clicks back from the next activity, the animation is started again. How can I prevent the animation from showing again as i want it to occur only once when user opens the app?

4

2 回答 2

1

You can set a flag for this, first time keep the flag true and when the user clicks on any of the button, set the flag value to false.

Now start the animation if the flag value is true.

于 2012-10-17T08:10:36.040 回答
1

You can use Application class.Declare a boolean var in it and set it "true" before starting second Activity.In your onCreate() of first Activity check this boolean and do animation only it is false(it means that user has not started second activity yet).For example create class with name App in your package:

    public class App extends Application{    

    private static boolean animated;

    @Override
    public void onCreate() {
        super.onCreate();
        animated = false;
    }

    public static boolean getAnimated(){
        return animated;
    }

    public static void setAnimated(boolean animated1){
        animated = animated1;
    }

}

Register App in manifest:

<application
        android:icon= ...
        android:label= ...
        android:name="yourpackage.name.App" >

(I suppose that your package name is :"yourpackage.name")

Now change your code like this:

if (!App.getAnimated()) {
            App.setAnimated(true);
            revolution.setVisibility(View.INVISIBLE);
            login.setVisibility(View.INVISIBLE);
            signUp.setVisibility(View.INVISIBLE);
            ...

Or you can use sharedpreferences and retrieve a boolean from it when you want to start animation.You have to set it's default value "false" and when user start second Activity,you have to set it "true".

于 2012-10-17T08:11:15.273 回答