title: How to implement onBackPressed() & intents in fragment?
body: <p>I know that onBackPressed() is a method in activity but, I want to use the functionality in fragments such that when back button is pressed, it gets redirected to another activity via Intent. Is there any solution to this ?</p>
  
  <pre><code>public class News_Events_fragment extends Fragment {
  ProgressDialog pd;
  ListView lv1;
  SharedPreferences sharedPreferences = null;
  int NotiCount;
  TextView txt_title, txt_msg, textView;
  Context context;
  Intent intent ;
  ArrayList&lt;SliderMsgTitleModel&gt; CurrentOfficersPastList;
  NewsActivityAdapter pastAdapter;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      context = (Context) getActivity();
  
      View rootView = inflater.inflate(R.layout.activity_news, container, false);
  
      new AsyncTask&lt;Void, Void, ArrayList&lt;SliderMsgTitleModel&gt;&gt;() {
  
          protected void onPreExecute() {
              pd = new ProgressDialog(getActivity());
              pd.setCancelable(true);
              pd.setTitle("UPOA");
              pd.setMessage("Please wait,loading the data...");
              pd.show();
          }
  
          @Override
          protected ArrayList&lt;SliderMsgTitleModel&gt; doInBackground(
                  Void... params) {
              System.out.println("In Background");
              CurrentOfficersPastList = new ArrayList&lt;SliderMsgTitleModel&gt;();
              // display view for selected nav drawer item
              ParseQuery&lt;ParseObject&gt; query = ParseQuery.getQuery("message");
              query.whereEqualTo("featured_status", true);
              // query.whereEqualTo("push_status", true);
  
              query.orderByDescending("updatedAt");
  
              query.selectKeys(Arrays.asList("title"));
              query.selectKeys(Arrays.asList("message"));
              try {
                  query.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
                  List&lt;ParseObject&gt; results = query.find();
                  for (int i = 0; i &lt; results.size(); i++) {
                      ParseObject object = results.get(i);
                      CurrentOfficersPastList.add(new SliderMsgTitleModel(
                              object.getString("title"), object
                                      .getString("message")));
                      System.out.println("title is=="
                              + object.getString("title") + "&amp;&amp; message is"
                              + object.getString("message") + "size is"
                              + CurrentOfficersPastList.size());
  
                  }
              } catch (Exception e) {
                  e.getMessage();
              }
              pd.dismiss();
  
              return CurrentOfficersPastList;
          }
  
          @SuppressWarnings("unchecked")
          @Override
          protected void onPostExecute(ArrayList&lt;SliderMsgTitleModel&gt; value) {
  
              pd.dismiss();
              /*Intent ent = new Intent(getActivity(), NewsActivity.class);
              ent.putExtra("NEWSLIST", (ArrayList&lt;SliderMsgTitleModel&gt;) value);
              startActivity(ent);
              System.out.println("Value is" + value.size());*/
  
              CurrentOfficersPastList = new ArrayList&lt;SliderMsgTitleModel&gt;();
              CurrentOfficersPastList = value;
              lv1 = (ListView) getActivity().findViewById(R.id.list_title);
              pastAdapter = new NewsActivityAdapter(getActivity(), R.layout.activity_news_txt, CurrentOfficersPastList);
              lv1.setAdapter(pastAdapter);
  
          }
      }.execute();
  
      return rootView;
  }
  
  public void onBackPressed() {
      // TODO Auto-generated method stub
      //super.onBackPressed();
      //Toast.makeText(getApplicationContext(), "click",2000).show();
              String cameback="CameBack";
              intent = new Intent(getActivity(),HomeActivity.class);
              intent.putExtra("Comingback", cameback);
              startActivity(intent);
  }
  
   }
  </code></pre>