android splash screen controls back button with onKeyDown

143 views Asked by At

i have splash screen activity and after 3 sec second activity is starting.this is a first activity code

Handler handler=new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(in);

                overridePendingTransition(R.anim.trans_left_in,
                        R.anim.trans_left_out);




        }
    }, 3000);

and in MainActivity activity i wrote this method

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {

        onBackPressed();
    }

    return super.onKeyDown(keyCode, event);
}

i have a problem.when i am second activity and click back button ,then i can't start second activity after 3 sec.how i can write code to can start again second activity?

2

There are 2 answers

0
Haresh Chhelana On BEST ANSWER

Move start intent code in onResume :

@Override
protected void onResume() {
   super.onResume();
   Handler handler=new Handler();
   handler.postDelayed(new Runnable() {
     @Override
     public void run() {
        Intent in = new Intent(this,MainActivity.class);
        startActivity(in);
        overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
     }
    }, 3000);
}
0
aletede91 On

You can finish the first activity:

finish();

Then, in the second activity, you can re-start the first activity with this intent:

Intent i = new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
finish();