onKeyDown() or onBackPressed()

前端 未结 4 1958
栀梦
栀梦 2020-12-06 05:10

I want to implement the back button functionality in my application. In application whenever I\'m clicking on back button in middle my control is going to login page direct

相关标签:
4条回答
  • 2020-12-06 05:39

    Yes you can override that back button

    public void onBackPressed() {
    
         Intent start = new Intent(currentclassname.this,which activity u want.class);
            startActivity(start);
            finishActivity(0);
            }
    

    By this you can move on any activity. This is very easy and simple way

    0 讨论(0)
  • 2020-12-06 05:40

    see below code. write outside the onCreate

      @Override  
        public boolean onKeyDown(int keyCode, KeyEvent event)  
      {  
             //replaces the default 'Back' button action  
             if(keyCode==KeyEvent.KEYCODE_BACK)  
             {  
    
                    Intent intent = new Intent(currentActivity.this, RequiredActivity.class);
                    finish();
                    startActivity(intent); 
    
             }  
             return true;  
       }  
    
    0 讨论(0)
  • 2020-12-06 05:43

    If your concern is about removing the login activity from the history stack.

    Execute finish(); in your login activity when you start any other activity from that

    0 讨论(0)
  • 2020-12-06 05:53

    Depends on whether or not you want to support pre-Android 2.0 phones. The onBackPressed() method was added to Android 2.0 (API 5).

    You may want to read this post on the Android Developer blog for details:

    http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

    0 讨论(0)
提交回复
热议问题