title: android control back button click in fragment
body: <p>i have my sidemenu and i also have some fragments and i replaced my fragments this is my source:</p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
  public static ScrollerLinearLayout sideSlideLayout;
  public static ListView slidemenulistview;
  private StradaSlideMenuAdapter slidemenuadapter;
  
  
  public static int fragmentposition=0;
  private String[] menu_namesGeo = {
  
  "Home", "Door", "street"};
  
  
  int[] menu_images = {
  
  R.drawable.menunewsimg, R.drawable.menuchartimg, R.drawable.menunearimg
  
  };
  
  public static ImageView menuButton;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_main);
  
      sideSlideLayout = (ScrollerLinearLayout) findViewById(R.id.menu_content_side_slide_layout);
      menuButton = (ImageView) findViewById(R.id.main_tmp_button);
  
      slidemenulistview = (ListView) findViewById(R.id.slidemenulistview);
      slidemenulistview.setEnabled(false);
  
  
  
      menuButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              slidemenulistview.setEnabled(true);
  
              sideSlideLayout.scroll();
  
          }
      });
  
      slidemenuadapter = new StradaSlideMenuAdapter(MainActivity.this,
              menu_namesGeo, menu_images);
  
      slidemenulistview.setAdapter(slidemenuadapter);
      slidemenulistview.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                  int possition, long arg3) {
              //btn_categorry.setEnabled(true);
              navigateTo(possition);
  
              sideSlideLayout.scroll();
          }
      });
  
      navigateTo(0);
  
  }
  
  private void navigateTo(int position) {
  
      switch (position) {
  
      case 0:
  
          getSupportFragmentManager()
                  .beginTransaction()
                  .replace(R.id.content_frame,
                          MainPageViewpager.newInstance(),
                          MainPageViewpager.TAG).commit();
          fragmentposition=0;
          break;
  
      case 1:
  
          getSupportFragmentManager()
                  .beginTransaction()
                  .replace(R.id.content_frame,
                          DealBoxNearFragment.newInstance(),
                          DealBoxNearFragment.TAG).commit();
          fragmentposition=1;
          break;
  
  
      case 2:
  
          getSupportFragmentManager()
                  .beginTransaction()
                  .replace(R.id.content_frame,
                          DealBoxAddBalanceFragment.newInstance(),
                          DealBoxAddBalanceFragment.TAG).commit();
          fragmentposition=2;
          break;
  
  
  
      }
  }
  
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) 
  {
      if(keyCode == KeyEvent.KEYCODE_BACK &amp;&amp;fragmentposition==0)
      {
  
          Toast.makeText(getApplicationContext(), "First fragment", Toast.LENGTH_SHORT).show();
  
          return true;
      }
      else if(keyCode == KeyEvent.KEYCODE_BACK &amp;&amp;fragmentposition==1)
      {
          onBackPressed();
  
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>i wrote <code>onKeyDownlistener</code> but my code does not working compete. I need to control fragment 's replace each times.how i can write code? if anyone knows solution please help me
  thanks</p>