2012-08-22 4 views
-1

Activity으로 갈 때 누구나 Activity을 완전히 끝내는 방법을 알고 있습니까? 나는 그것을 끝내는 활동에 finish()을 뒀다. 그러나 전화 키 또는 에뮬레이터에서 뒤로 키를 누르면 마지막으로 Activity이 열립니다. 예 :다른 활동을 할 때 활동을 분명히 끝내는 방법

액티비티 A는 메뉴이고 B는 주문이며 C는 주문이며 D는 지불입니다.

Activity A, Activity B에가는 Activity BActivity CActivity D에, Activity C합니다. 전화 또는 에뮬레이터의 뒤로 키를 누를 경우 Activity D에있을 때 C, B 및 A로 돌아갑니다. 그러나 올바른 번호는 Activity D에서 Activity A으로 변경됩니다. 다시 키를 누르면 앱이 닫힙니다.

누구나 활동을 명확하게 종료하는 방법을 알고 있습니까?

감사합니다.

다른 프로그램이 있습니다. 게임 메뉴가 있습니다. 메뉴의 뒤쪽으로 내 게임 레벨가는

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 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 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에서 Activity B을 실행하려면 startActivity으로 전화해야하지만 finish()으로 전화하지 않아야합니다. Activity B에서 Activity C으로 갈 시간이되면 적절한 의도로 startActivity으로 전화를 걸어 finish()으로 즉시 전화해야합니다. 예를 들어 : Activity C에 대한 마찬가지로

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

Activity D를 시작하는 시간입니다. 그런 다음 Activity D에서 돌아 오면 Activity A이 다시 표시됩니다.

이미 그렇게하고 있다고 생각되면 코드를 게시하시기 바랍니다.

+0

내 코드를 게시하고 친절하게 확인해주세요. 도움에 감사드립니다. –

관련 문제