2012-08-22 134 views
-1

任何人都知道如何完全結束Activity時去另一個Activity?我在活動上放置了一個finish()來終止它。但是當我按下電話的後退鍵或模擬器時,它會回到最後的Activity我打開。例如:如何清楚地結束活動時去其他活動

活動A是菜單,B是訂單,C也是訂單,D是付款。

Activity AActivity BActivity BActivity CActivity CActivity D。 如果我按手機或模擬器的後退鍵當我在Activity D它回到C,B和A.但正確的應該從Activity D回到Activity A。如果我再次按下後退鍵,它將關閉應用程序。

任何人都知道如何清楚地結束Activity?

謝謝!

我有一個不同的程序,這裏是我的遊戲菜單。

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

當我按下電話的後退按鈕模擬器,它會返回到我打開的最後一個活動。如果我打開遊戲菜單----->指令----->回到菜單----->新遊戲---->遊戲關卡----->輕鬆遊戲,我按回來按鈕它回到我最近的開放活動。

如何解決這個問題?

回答

0

每次開始下一個活動時都不需要顯式調用完成,只需將android:noHistory =「true」設置爲您要從活動堆棧中移除的那些活動即可。

+0

如果您不希望活動保留在堆棧上,這是一個很好的解決方案。如果你只想在某些情況下它從堆疊中消失,那麼'noHistory'就太鈍了。 –

+0

這是OP想要的東西,從A到B(noHistory)到C(noHistory)到D.當按回到A時,再次按下應用退出 – Macchiato

+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

如果你認爲你已經這樣做了,那麼請發佈你的代碼,因爲你不是。

+0

我發佈我的代碼,請檢查一下,謝謝你的幫助。 –