如何在不破坏应用程序的情况下销毁活动

时间:2017-01-12 11:56:04

标签: android-studio

当按下主页按钮或应用程序处于后台时,我一直试图破坏活动,我设法用这段代码做某事



@Override
    protected void onPause() {
        this.finish();
        super.onPause();

    }




当应用程序运行时它不会崩溃但是在活动中玩了一段时间之后,应用程序就会出现背景并崩溃。 这是我正在谈论的活动:



   



public class Playing extends AppCompatActivity  implements View.OnClickListener {
    final static long INTERVAL=1154;//1 second
    final static long TIMEOUT=7000;
    int progressValue = 0;
    CountDownTimer mcountDown;
    List<Question> questionPlay = new ArrayList<>();
    DbHelper db;
    int index=0 , score =0,thisQuestion=0,totalQuestion,CorrectAnswer;
    String mode = "";
    ProgressBar progressBar;
    ImageView imageView;
    Button btnA, btnB, btnC,btnD;
    TextView txtScore,txtQuestion;

    @Override
    protected void onPause() {
        this.finish();
        super.onPause();

    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playing);

    Bundle extra = getIntent().getExtras();
        if(extra!=null)
        mode =extra.getString("MODE");
        db = new DbHelper(this);
        txtScore = (TextView)findViewById(R.id.txtScore);
        txtQuestion =(TextView)findViewById(R.id.txtQuestion);
        progressBar=(ProgressBar) findViewById(R.id.progreessBar);
        btnA = (Button) findViewById(R.id.btnAnswerA);
        btnB =(Button)findViewById(R.id.btnAnswerB);
        btnC = (Button)findViewById(R.id.btnAnswerC);
        btnD = (Button)findViewById(R.id.btnAnswerD);
        imageView = (ImageView)findViewById(R.id.question_bike);

btnA.setOnClickListener(this);
        btnB.setOnClickListener(this);
        btnC.setOnClickListener(this);

        btnD.setOnClickListener(this);
    }






    @Override
    protected void onResume() {
        super.onResume();

        questionPlay = db.getQuestionMode(mode);
        totalQuestion = questionPlay.size();
        mcountDown = new CountDownTimer(TIMEOUT, INTERVAL) {


            @Override
            public void onTick(long millisUntilFinished) {
                progressBar.setProgress(progressValue);
                progressValue++;


            }




            @Override
            public void onFinish() {
                mcountDown.cancel();
                showQuestion(++index);

            }
        };
        showQuestion(index);


    }



    private void showQuestion(int index) {
        if (index < totalQuestion){
            thisQuestion++;
            txtQuestion.setText(String.format("%d %d",thisQuestion,totalQuestion));
            progressBar.setProgress(0);
            progressValue = 0;
            int ImageID = this.getResources().getIdentifier(questionPlay.get(index).getImage().toLowerCase(),"drawable",getPackageName());
            imageView.setBackgroundResource(ImageID);
            btnA.setText(questionPlay.get(index).getAnswerA());
            btnB.setText(questionPlay.get(index).getAnswerB());
            btnC.setText(questionPlay.get(index).getAnswerC());
            btnD.setText(questionPlay.get(index).getAnswerD());
            mcountDown.start();


        }
else {
            Intent intent = new Intent(this,Done.class);
            Bundle dataSend = new Bundle();
            dataSend.putInt("SCORE", score);
            dataSend.putInt("TOTAL",totalQuestion);
            dataSend.putInt("CORRECT",CorrectAnswer);
            intent.putExtras(dataSend);
            startActivity(intent);
            finish();

        }

    }

    @Override
    public void onClick(View v) {
        mcountDown.cancel();
        if (index < totalQuestion){
            Button clickedButton = (Button)v;
            if(clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())){
                score+=1;
                CorrectAnswer++;
                showQuestion(++index);

            }
        else showQuestion(++index);
            txtScore.setText(String.format("S:%d",score));
        }

    }





    @Override
    public void onBackPressed() {

        Intent intent = new Intent(Playing.this ,MainActivity.class) ;
        startActivity(intent) ;
        finish() ;


    }



}
&#13;
&#13;
&#13; 我试过这个代码

&#13;
&#13;
@Override
protected void onDestroy(){
super.onDestroy;
  finish();
}
&#13;
&#13;
&#13;

但是第二个活动没有启动,应用程序再次崩溃。 那该怎么办?

0 个答案:

没有答案