-2

i try to close the sliding drawer when i click on the return back key but its not working here is the code i used

@Override
public void onBackPressed() {
   Log.d("onBackPressed Called", FROM_SETTINGS_KEY);
      slidingDrawer.close();

}

the xml :

<SlidingDrawer
    android:id="@+id/slidingDrawer1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="197dp"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Handle" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>
</SlidingDrawer>

so how can i make the return back key close this sliding drawer ?

user3425815
  • 25
  • 1
  • 8
  • Are you throwing an `Exception` or does the `SlidingDrawer` not respond? – adneal Apr 05 '14 at 17:00
  • the slidingdrawer not respond and i make the mainactivity not respond to the back key by this code public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { } return false; – user3425815 Apr 05 '14 at 17:11

1 Answers1

1

You're overriding Activity.onKeyDown, which Activity already overrides when implementing Activity.onBackPressed, then returning false for KeyEvent.KEYCODE_BACK which indicates that you have not handled this event and it should continue to be propagated.

To fix your problem stop overriding Activity.onKeyDown. Also, change your Activity.onBackPressed to call super if the SlidingDrawer isn't opened.

private SlidingDrawer slidingDrawer;

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

    slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
}

@Override
public void onBackPressed() {
    if (slidingDrawer.isOpened()) {
        slidingDrawer.close();
    } else {
        super.onBackPressed();
    }
}
user3425815
  • 25
  • 1
  • 8
adneal
  • 30,484
  • 10
  • 122
  • 151
  • @user3425815 Was this the answer you were looking for? Did you need more help? – adneal Apr 05 '14 at 18:37
  • @user3425815 That doesn't make sense. As long as you're not interfering with the `KeyEvent.KEYCODE_BACK` event, calling `SlidingDrawer.close` in `Activity.onBackPressed` will most definitely close it. Are you possibly forgetting to mention something else that might interfere with the "back" press? – adneal Apr 06 '14 at 13:05
  • here is the code i used @Override public void onBackPressed() { if (slidingDrawer.isOpened()) { slidingDrawer.close(); } else { super.onBackPressed(); } } public boolean onBackPressed(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { } return false; } } its give me force close after click on return back button – user3425815 Apr 06 '14 at 13:11
  • @user3425815 Remove **all** the code that deals with `KeyEvent.KEYCODE_BACK`. Only override `onBackPressed` exactly like my answer. – adneal Apr 06 '14 at 13:17
  • @Override public void onBackPressed() { if (slidingDrawer.isOpened()) { slidingDrawer.close(); } else { super.onBackPressed(); } }} and when i clicked on the return back button gives me force close again and i removed the keyevent code – user3425815 Apr 06 '14 at 13:24
  • @user3425815 I made an edit, make sure you're initializing the `SlidingDrawer` after you call `setContentView`. I'm assuming you're throwing a `NullPointerException`. – adneal Apr 06 '14 at 13:33
  • it works thank you very much you was right but its only asked to add cast to sliding drawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1); and works fine .. thank you again very much – user3425815 Apr 07 '14 at 14:13