如何在进入另一项活动时清楚地结束活动

时间:2012-08-22 02:03:17

标签: android

任何人都知道如何在转到另一个Activity时完全结束Activity?我在活动上放了一个finish()来终止它。但是当我按下电话的后退键或模拟器时,它会回到最后Activity我打开。例如:

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

Activity A转到Activity BActivity B转到Activity CActivity C转到Activity D。 如果我按下电话的后退键或模拟器当我在Activity D时,它会返回到C,B和A.但是正确的应该从Activity D返回到{{ 1}}。如果我再次按后退键,它将关闭应用程序。

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

谢谢!

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

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();
        }
       }

当我按下电话或模拟器的后退按钮时,它会返回到我打开的最后一个活动。如果我打开游戏菜单-----&gt;指令-----&gt;回到菜单-----&gt;新游戏----&gt;游戏关卡-----&gt;简单的游戏,我按回按钮,它回到我最近开放的活动。

如何解决这个问题?

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

每次启动下一个活动时都不需要显式调用结束,只需将android:noHistory =“true”设置为要从活动堆栈中删除的活动。