-1

Activity有谁知道去另一个地方时如何完全结束Activity?我finish()在活动上放了一个来终止它。但是当我按下手机的后退键或在模拟器中时,它会回到Activity我打开的最后一个。例如:

Activity A 是菜单,B 是订单,C 也是订单,D 是付款。

Activity AActivity BActivity BActivity CActivity CActivity D。如果我按下手机或模拟器的返回键,当我在其中时,Activity D它会返回到 C、B 和 A。但正确的应该是从Activity D返回到Activity A。如果我再次按返回键,它将关闭应用程序。

任何人都知道如何清楚地结束活动?

谢谢!

我有一个不同的程序,这是我的游戏菜单。

public class BodyPartsGameActivity extends Activity {

    protected boolean _active = true;
    protected int _splashTime = 1000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
        }


    MediaPlayer mp1;

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


        private void StartAnimations() {
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
            anim.reset();
            LinearLayout l=(LinearLayout) findViewById(R.id.mainlay);
            l.clearAnimation();
            l.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.clouds);
            anim.reset();
            Button iv = (Button) findViewById(R.id.cloud);
            iv.clearAnimation();
            iv.startAnimation(anim);





        mp1 = MediaPlayer.create(this,R.raw.officialbackgroundmusic);
        mp1.start();

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.button);
        final Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);



        Button newgameButton = (Button)findViewById(R.id.newgame);
        newgameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent newgameIntent = new Intent(BodyPartsGameActivity.this,pgone.class);
                startActivity(newgameIntent);
                    mp.start();
                    mVibrator.vibrate(500);       
            }
        });

        final MediaPlayer ms = MediaPlayer.create(this, R.raw.button);

        Button highscoresButton = (Button)findViewById(R.id.highscores);
        highscoresButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent highscoresIntent = new Intent(BodyPartsGameActivity.this,highscores.class);
                startActivity(highscoresIntent);
                 ms.start();
                 mVibrator.vibrate(500);
            }
        });


        Button instructionButton = (Button)findViewById(R.id.settings);
       instructionButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent instructionsIntent = new Intent(BodyPartsGameActivity.this,settingstwo.class);
                startActivity(instructionsIntent);
                 mp.start();
                 mVibrator.vibrate(500);
            }
        });


       Button instructionsButton = (Button)findViewById(R.id.instructions);
      instructionsButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent instructionsIntent = new Intent(BodyPartsGameActivity.this,instructions.class);
            startActivity(instructionsIntent);
         ms.start();
        mVibrator.vibrate(500);
        }
       });



      final ImageView mNotification_on_btn=(ImageView)findViewById(R.id.on_btn);
      final ImageView mNotification_off_btn=(ImageView)findViewById(R.id.off_btn);

          mNotification_on_btn.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      mNotification_on_btn.setVisibility(View.GONE);
                      mNotification_off_btn.setVisibility(View.VISIBLE);
                  }
              });
          ImageView pausresumeButton = (ImageView)findViewById(R.id.on_btn);
         pausresumeButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                    if(mp1.isPlaying())
                    {
                    mp1.pause();
                    }
                    else
                    {
                    mp1.start();
                    }
                            }});


          mNotification_off_btn.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      mNotification_off_btn.setVisibility(View.GONE);
                      mNotification_on_btn.setVisibility(View.VISIBLE);
                  }
              });


    }

        @Override
        protected void onPause() {
          if (this.isFinishing()){ //basically BACK was pressed from this activity
            mp1.stop();
            Toast.makeText(BodyPartsGameActivity.this, "You Pressed Back Button on Your 'HOME'  The Game was ended.  ", Toast.LENGTH_SHORT).show();
          }
          Context context = getApplicationContext();
          ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
          List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
          if (!taskInfo.isEmpty()) {
            ComponentName topActivity = taskInfo.get(0).topActivity; 
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
              mp1.stop();
              Toast.makeText(BodyPartsGameActivity.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
            }

          }
              super.onPause();
              mp1.reset();
          }

        }

这是我的说明页面:

public class instructions extends Activity{

    MediaPlayer mPlayer;

    protected boolean _active = true;
    protected int _splashTime = 1000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
        }


    @Override
    public void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.instructions); 
        StartAnimations();

     }


    private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.instbg);
        l.clearAnimation();
        l.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controll);
        anim.reset();
        Button iv = (Button) findViewById(R.id.hwtoply);
        iv.clearAnimation();
        iv.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controlll);
        anim.reset();
        Button ib = (Button) findViewById(R.id.cntrls);
        ib.clearAnimation();
        ib.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controllll);
        anim.reset();
        Button ic = (Button) findViewById(R.id.trivia);
        ic.clearAnimation();
        ic.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controlllll);
        anim.reset();
        Button iz = (Button) findViewById(R.id.about);
        iz.clearAnimation();
        iz.startAnimation(anim);


        Button back4Button = (Button)findViewById(R.id.back4);
        back4Button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent back4Intent = new Intent(instructions.this,BodyPartsGameActivity.class);
                startActivity(back4Intent);
                finish();

            }
         });


        Button howtoButton = (Button)findViewById(R.id.hwtoply);
        howtoButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent howtoIntent = new Intent(instructions.this,howto.class);
                startActivity(howtoIntent);


            }
        });

        Button ctrlsButton = (Button)findViewById(R.id.cntrls);
        ctrlsButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent ctrlsIntent = new Intent(instructions.this,controls.class);
                startActivity(ctrlsIntent);


            }
        });

        Button triviaButton = (Button)findViewById(R.id.trivia);
        triviaButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent triviaIntent = new Intent(instructions.this,trivia.class);
                startActivity(triviaIntent);


            }
        });

        Button abwtButton = (Button)findViewById(R.id.about);
        abwtButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent abwtIntent = new Intent(instructions.this,about.class);
                startActivity(abwtIntent);


            }
        });




}
    @Override
    protected void onDestroy() {
        setResult(2);
        super.onDestroy();
    }
}

回到菜单然后我的游戏关卡:

public class gamelevel extends Activity { 
    protected boolean _active = true;
    protected int _splashTime = 1000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
        }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamelevel); 

        StartAnimations();   
    }
        private void StartAnimations() {
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
            anim.reset();
            LinearLayout l=(LinearLayout) findViewById(R.id.gmlvl);
            l.clearAnimation();
            l.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.controll);
            anim.reset();
            Button iv = (Button) findViewById(R.id.easypg);
            iv.clearAnimation();
            iv.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.controlll);
            anim.reset();
            Button ib = (Button) findViewById(R.id.mediumpg);
            ib.clearAnimation();
            ib.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.controllll);
            anim.reset();
            Button ic = (Button) findViewById(R.id.hard);
            ic.clearAnimation();
            ic.startAnimation(anim);


        Button easypButton = (Button)findViewById(R.id.easypg);
        easypButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent easypIntent = new Intent(gamelevel.this,Maingame.class);
                startActivity(easypIntent);
                finish();
            }
        });

        Button mediumButton = (Button)findViewById(R.id.mediumpg);
        mediumButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent mediumIntent = new Intent(gamelevel.this,Mediumgame.class);
                startActivity(mediumIntent);
                finish();
            }
        });

        Button hardButton = (Button)findViewById(R.id.hard);
        hardButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent hardIntent = new Intent(gamelevel.this,Hardgame.class);
                startActivity(hardIntent);
                finish();
            }
        });

        Button back1Button = (Button)findViewById(R.id.back1);
        back1Button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent back1Intent = new Intent(gamelevel.this,BodyPartsGameActivity.class);
                startActivity(back1Intent);
                finish();
            }
        });
    }
        @override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK)
            {
                moveTaskToBack(true);
                return true; // return
            }

            return false;

         }
        @Override
        protected void onDestroy() {

            super.onDestroy();
        }
       }

这是我的简单游戏:

public class Maingame extends Activity {
    int v,x,y,z;



    public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
             displayIntentData();

////////////////////////////////score calculation/////////////////////
             initControls();
      }
      private void initControls()
      {
          amount1=(EditText)findViewById(R.id.amount1);
          amount2=(EditText)findViewById(R.id.amount2);
          amount3=(EditText)findViewById(R.id.amount3);
          result=(TextView)findViewById(R.id.result);
          calculate=(Button)findViewById(R.id.finish);
          calculate.setOnClickListener(new Button.OnClickListener()
          {public void onClick
          (View  v) { calculate();}});
      }
      private void calculate()
      {

       if(amount1.getText().toString().equals(""))
          {
          x=0;
          }
       else
          {
          x=Integer.parseInt(amount1.getText().toString());
            }

       if(amount2.getText().toString().equals("")) 
          {
          y=0; 
            }
       else
          {
          y=Integer.parseInt(amount2.getText().toString());
            }
       if(amount3.getText().toString().equals(""))
          {
          v=0;
            }
       else
          {
       v=Integer.parseInt(amount3.getText().toString());
            }

          z=x+y+v;
          result.setText(Integer.toString(z));


      ////////////////////////////////score calculation/////////////////////

                findViewById(R.id.finish).setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        Intent intent = new Intent(Maingame.this,score.class);
                        intent.putExtra("key",((TextView)findViewById(R.id.result)).getText().toString());
                        startActivity(intent);
                    }
                });
            }
            @Override
            protected void onNewIntent(Intent intent) {
                super.onNewIntent(intent);
                setIntent(intent);
                displayIntentData();
            }

            private void displayIntentData(){
                Intent intent = getIntent();
                TextView tv = (TextView)findViewById(R.id.intentData);
                Bundle extras=intent.getExtras();


                if(extras!=null){
                    tv.setText(" "+extras.getString("key"));
                }else{
                    tv.setText("");
                }
            }



    private Gallery gallery;

    TextView select1;
    TextView select2;
    TextView select3;

    TextView result;

     EditText amount1;
     EditText amount3;
     EditText amount2;
     EditText pauseb;


     Button calculate;

    @Override
    public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.maingame);


     Button pauseButton = (Button)findViewById(R.id.pause);
     pauseButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent backcreIntent = new Intent(Maingame.this,pauseg.class);
            startActivity(backcreIntent);

        }
       });

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView parent, View v, int position, long id) {

        if (position == 0 ) {
            //Toast.makeText(Maingame.this, "heart", Toast.LENGTH_SHORT).show();
             select1 = (TextView) findViewById(R.id.txt1);
              select1.setText("Head");

            amount1.setText("1000");

        } else if (position == 1) {

            select1 = (TextView) findViewById(R.id.txt1);
              select1.setText("Eye");

              amount1.setText("0");

        } else if (position == 2) {

            select1 = (TextView) findViewById(R.id.txt1);
              select1.setText("Intestine");

              amount1.setText("0");

        } else if (position == 3) {

            select2 = (TextView) findViewById(R.id.txt2);
              select2.setText("Ear");

              amount2.setText("0");

        } else if (position == 4) {

            select2 = (TextView) findViewById(R.id.txt2);
              select2.setText("liver");

              amount2.setText("0");

        } else if (position == 5) {

            select2 = (TextView) findViewById(R.id.txt2);
              select2.setText("Arm");

              amount2.setText("1000");

        } else if (position == 6) {

            select3 = (TextView) findViewById(R.id.txt3);
              select3.setText("Hand");

              amount3.setText("0");
              Toast.makeText(Maingame.this, "Click Finish To Submit Your Answer", Toast.LENGTH_LONG).show();

        } else if (position == 7) {

            select3 = (TextView) findViewById(R.id.txt3);
              select3.setText("Foot");

              amount3.setText("0");
              Toast.makeText(Maingame.this, "Click Finish To Submit Your Answer", Toast.LENGTH_LONG).show();

        } else if (position == 8) {

            select3 = (TextView) findViewById(R.id.txt3);
              select3.setText("Legs");

              amount3.setText("1000");
              Toast.makeText(Maingame.this, "Click Finish To Submit Your Answer", Toast.LENGTH_LONG).show();

        }
    }
    });


    Button clear = (Button)findViewById(R.id.clear);
    clear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ((TextView) findViewById(R.id.txt1)).setText("");
            ((TextView) findViewById(R.id.txt2)).setText("");
            ((TextView) findViewById(R.id.txt3)).setText("");
        }
    });

    }


    public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
    R.drawable.head,R.drawable.eye, R.drawable.largeintestine,R.drawable.ear, R.drawable.liver, R.drawable.arm
    ,R.drawable.hand,R.drawable.foot,R.drawable.legs};

    public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.BodyPartsGame);
    GalItemBg = typArray.getResourceId(R.styleable.BodyPartsGame_android_galleryItemBackground, 0);
    typArray.recycle();
    }

    public int getCount() {
    return Imgid.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    // Fixing width & height for image to display
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
    }
    }
    }

当我按下手机或模拟器的后退按钮时,它会回到我打开的最后一个活动。如果我打开游戏菜单 -----> 说明 -----> 返回菜单 -----> 新游戏 ----> 游戏关卡 -----> 简单游戏,然后按返回按钮回到我最近的开放活动。

如何解决?

4

2 回答 2

0

Activity A中,您应该调用startActivity启动Activity B但不应该调用finish(). 在Activity B,该去的时候Activity C,你应该startActivity以适当的意图打电话并立即打电话finish()。例如:

private void timeForActivityC() {
    Intent intent = new Intent(this, ActivityC.class);
    startActivity(intent);
    finish();
}

同样适用于Activity C何时启动Activity D。然后,当您从 回去时Activity D,您应该会Activity A再次看到。

如果你认为你已经这样做了,那么请发布你的代码,因为你不是。

于 2012-08-22T02:07:14.100 回答
0

您不需要在每次开始下一个活动时显式调用完成,只需将 android:noHistory="true" 设置为您要从活动堆栈中删除的那些活动。

于 2012-08-22T03:01:01.033 回答