6

Activity A has a button and in its onclick it starts new activity B, the new activity only has minimal onCreate function.

In activity A I push a button which uses

startActivity(new Intent(A.this, B.class)) 

to start activity B. when I run the program, push the button in activity A, activity B starts but I have to use back button twice to exit and get back to the first activity.

I checked the logcat when the button in A is clicked, two same instances are made simultaneously. Also I tried using Toast in onCreate in Activity B for debugging, which shows that when I use back button it calls the onCreate in the other twin B Activity. I found this post Clicking the back button twice to exit an activity which does not answer the question.

The only way that I could stop it from making twin instances was by explicitly limiting it using the tag:launchMode= "singleInstance" to the activity in manifest.

The questions still remain:

  1. why it makes two instances?
  2. How to stop it or how to use it correctly so that this wont happen?

More explanation: The code: Activity A has a buttOnClick() function in which there is a switch(view.getId()) that checks which button was clicked. each case calls function animation with a specific integer parameter (button id). in this function again switch checks the id and runs the animation corresponding to the button using setAnimationListener.At the end of the animation it asks for starting a new activity. Here is what it looks like:

public void myButtonOnClick(View view)
    {


        switch (view.getId()) {

        case R.id.button1:

            animation(1);

            break;
           //....more lines.....  }}
public void animation(int a){

           //...code...

          switch(a){

        case 1:
            anim.setAnimationListener(new AnimationListener() {

                public void onAnimationStart(Animation arg0) {
                    // TODO Auto-generated method stub

                }

                public void onAnimationRepeat(Animation arg0) {
                    // TODO Auto-generated method stub

                }

                public void onAnimationEnd(Animation arg0) {
                    Intent in =new Intent(A.this,     B.class);
                    startActivity(in.addFlags(      Intent.FLAG_ACTIVITY_CLEAR_TOP ));
                    animClear();
                }
            });


            break;
//......... other cases......}
Community
  • 1
  • 1
yav dat
  • 98
  • 1
  • 10
  • Please post more of your code. The problem is most likely that `startActivity()` is getting called twice somehow, or Activity B is starting another instance of itself. – devunwired Dec 29 '12 at 04:05
  • I don't see why two instances were made from Activity A. Can you please tell me @Devunwired how Activity B can make two instances of itself? (it does not start any activity) – yav dat Jan 31 '13 at 22:27

1 Answers1

3

Intent flags

I myself used a set of FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED but that might be an overkill.

Then just

startActivity(new Intent(A.this, B.class).addFlags(intent_flags));
aragaer
  • 17,238
  • 6
  • 47
  • 49
  • 2
    Thank you for your response. I tried and Intent.FLAG_ACTIVITY_CLEAR_TOP alone in my case stops the problem. But Can you explain where an unexpected behavior of making two activities instead of one comes from. Also, what benefit does your approach have in compare with adding tag:launchMode= "singleInstance" in manifest? At last, what is the best programming approach to this problem? Thank you. – yav dat Dec 29 '12 at 04:24
  • Unexpected behavior probably comes from error in the code. Speaking of flags and manifest.. I'd go and read [this](http://developer.android.com/guide/components/tasks-and-back-stack.html) - it looks like there's even some explanation of both approaches. – aragaer Dec 29 '12 at 04:34
  • Thank you it explains clearly how and when to use these approaches. – yav dat Dec 29 '12 at 04:51
  • ,Thank you it explains clearly how and when to use these approaches. I used a function which handles onClick for all the buttons in the activity using switch-case, after the case I handled the animation with animationlistner so that my second activity starts at the end of the animation. inside the onAnimationEnd I call startActivity. Could that be the possible error in the code? – yav dat Dec 29 '12 at 05:10