KrasivaM
KrasivaM

Reputation: 11

onBackPressed() doesn't work for me

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

    }

}

Upvotes: 0

Views: 774

Answers (1)

boudi
boudi

Reputation: 682

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

Upvotes: 1

Related Questions