title: How to intercept back button in a Fragment?
body: <p>I'm new to android programming and I need help: I need to intercept back button in a Fragment to show an exit-alert-dialog. I tried both with onBackPressed and onKeyDown, but they don't work, I don't know why...
  Here some code:</p>
  
  <pre><code>public class Home extends Fragment {
      private Dialog dialog;
     // onAttach : set activity listener
     @Override
     public void onAttach(Activity activity) {
        super.onAttach(activity);
        // if implemented by activity, set listener
        // else create local listener (code never executed in this example)
     }
  
     // onCreateView : 
     @Override
     public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  
        // fragment not when container null
        if (container == null) {
           return null;
        }
        // inflate view from layout
        final View view = inflater.inflate(R.layout.home,container,false);
  
          ...
  
          //onBackPressed(); doesn't work, alertDialog comes up only the first time without touching back button
        return view;
     }
  </code></pre>
  
  <p>My "onBackPressed"...</p>
  
  <pre><code>public void onBackPressed() {
              new AlertDialog.Builder(getActivity())
                  .setIcon(android.R.drawable.ic_dialog_alert)
                  .setTitle("Hey!")
                  .setMessage("Exit?")
                  .setPositiveButton("Yes", new DialogInterface.OnClickListener()
              {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      FragmentActivity i=getActivity(); 
                      i.finish();
                  }
  
              })
              .setNegativeButton("No", null)
              .show();
          }
  
      }
  </code></pre>
  
  <p>Here for you also PagerTabsActivity and PagerAdapter, if you need...
  PagerTabsActivity:</p>
  
  <pre><code>public class PagerTabsActivity extends FragmentActivity {
      private static int i=0;
      private static View pagerStrip;
      // list contains fragments to instantiate in the viewpager
      List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;();
      List&lt;String&gt; fragmentTitles = new Vector&lt;String&gt;();
      // page adapter between fragment list and view pager
      private PagerAdapter mPagerAdapter;
      // view pager
      private ViewPager mViewPager;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          // creating fragments and adding to list
          fragments.add(Fragment.instantiate(this, Home.class.getName()));
          fragmentTitles.add(Home.class.getSimpleName());
          fragments.add(Fragment.instantiate(this, Second.class.getName()));
          fragmentTitles.add(Second.class.getSimpleName());
          fragments.add(Fragment.instantiate(this, Third.class.getName()));
          fragmentTitles.add(Third.class.getSimpleName());
          //
          mViewPager = (ViewPager) findViewById(R.id.pager);
          pagerStrip = findViewById(R.id.pagerTabStrip);
          if (pagerStrip instanceof PagerTabStrip) {
              PagerTabStrip pagerTabStrip = (PagerTabStrip) pagerStrip;
              pagerTabStrip.setDrawFullUnderline(true);
              pagerTabStrip.setTabIndicatorColorResource(android.R.color.white);
              // pagerTabStrip.setTextColor(color.white);
          } else if (pagerStrip instanceof PagerTitleStrip) {
              PagerTitleStrip pagerTitleStrip = (PagerTitleStrip) pagerStrip;
              pagerTitleStrip.setTextColor(getResources().getColor(android.R.color.white));
          }
  
          this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
          mViewPager.setAdapter(this.mPagerAdapter);
          mViewPager.setCurrentItem(0);
      }
  }
  </code></pre>
  
  <p>PagerAdapter:</p>
  
  <pre><code>public class PagerAdapter extends FragmentPagerAdapter {
      // private String titles[] = new String[] { "Tab1", "Tab2", "Tab3" };
      private List&lt;String&gt; fragmentTitles;
      private List&lt;Fragment&gt; fragments;
  
      public PagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) {
          super(fm);
          this.fragments = fragments;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
  
          if (null == fragmentTitles || fragmentTitles.size() &lt;= position) {
              return " ";
          }
          String fragTitle = fragmentTitles.get(position);
          if (null == fragTitle || fragTitle.trim().length() == 0) {
              return "No Title";
          }
          // Log.v("TitleAdapter - getPageTitle=", fragmentTitles.get(position));
          return fragmentTitles.get(position);
      }
  
      @Override
      public Fragment getItem(int position) {
          // Log.v("TitleAdapter - getItem=", String.valueOf(position));
          return this.fragments.get(position);
      }
  
      @Override
      public int getCount() {
          return this.fragments.size();
      }
  
  }
  </code></pre>
  
  <p>Any help will be greatly appreciated! :)</p>