0

I have a problem, which is also discussed here: onBackPressed never gets called

I'm trying to cancel a CountDownTimer when pressing the native back button from an android phone. So I would like to overrite the onBackPressed method, to cancel the timer and return to another activity, but only once. (to return to the main activity, like a home button).

This is a part of the code:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

   if (getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE){
        ImageView und1=(ImageView)findViewById(R.id.undita1);
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) und1.getLayoutParams();
        params.height=150;
        und1.setLayoutParams(params);
        ImageView und2=(ImageView)findViewById(R.id.undita2);
        ViewGroup.LayoutParams params2 = (ViewGroup.LayoutParams) und2.getLayoutParams();
        params2.height=150;
        und2.setLayoutParams(params2);
        ImageView und3=(ImageView)findViewById(R.id.undita3);
        ViewGroup.LayoutParams params3 = (ViewGroup.LayoutParams) und3.getLayoutParams();
        params3.height=150;
        und3.setLayoutParams(params3);
    }

        mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
        mProgressBar.setProgress(0);
        mCountDownTimer=new CountDownTimer(90000,1000) {
            int i=0;
            @Override
            public void onTick(long millisUntilFinished) {
                Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
                i++;
                mProgressBar.setProgress(i); }
            @Override
            public void onFinish() {
                i++;
                mProgressBar.setProgress(i);
                Intent in = new Intent(getApplicationContext(), MyActivity2.class);
                startActivity(in);
            }
        };
        mCountDownTimer.start();

        android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Bundle b;
        b = getIntent().getExtras();
        nivel= b.getInt("level");
        TextView niv=(TextView) findViewById(R.id.Nivel);
        niv.setText("Level: "+ nivel);
        generare_3_diferente(nivel);}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.my, menu);
        return true;}
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

     @Override


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
            mCountDownTimer.cancel();
            Intent in = new Intent(getApplicationContext(), MyActivity2.class);
            startActivity(in);
            mCountDownTimer.cancel();
            return true;
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }


    @Override
    public void onBackPressed(){
        mCountDownTimer.cancel();
        Intent in = new Intent(this, MyActivity2.class);
        startActivity(in);

        super.onBackPressed();

    }

}
4
  • and what exactly is the problem? Commented Sep 17, 2014 at 14:09
  • Part of an answer of a related Question: "if you override both, onBackPressed() and onKeyDown(), both will catch the back press with onKeyDown catching it first. If you call super.onKeyDown in onKeyDown, then the onBackPressed method will fire. If you do not call super.onKeyDown then onBackPressed will never be called." Is this really what you want? Commented Sep 17, 2014 at 14:19
  • No, I thought that the 2 functions cover all the versions of Android... I want to stop the timer somehow and to make the back button to return only once to the main activity, the one where the user can choose between the levels of a game.
    – KrasivaM
    Commented Sep 17, 2014 at 14:41
  • The problem is that the functions aren't called. so the back button goes to activity2 and after that, if I press the back button again, it goes to the activity 1, and then to the activity2. And the timer doesn't stop when I go from Activity 1 to activity 2. And when I come to activity 1, there are more timers running in the same time. And some timer that wasn't stopped earlier is closing my game, even if the current timer is not finished. I would like to solve this problem, my game stops all the time because of this. And I'm using a CountDownTimer with a ProgressBar to count the seconds/level
    – KrasivaM
    Commented Sep 17, 2014 at 14:44

1 Answer 1

1

the onBackPressed should be in the main activity class

Also try to specify the parent and child activities in the manifest file of the app e.g

android:parentActivityName

and try to use something like this and rebuild your App

@Override 
public void onBackPressed() {
super.onBackPressed();
i = new Intent(CurrentActivity.this, NextActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(i);
finish();
} 

if it still doesn't work try to make a new application and import your files

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.