How to implement the onBackPressed () method in a class that inherits from Fragment, causing it to go back to the previous Fragment?

-2
Segue um trecho de código para exemplificar meu problema:



public class DashboardFragment extends Fragment{
   ...

}


@Override
public void onBackPressed(){
   //Aqui volta para o Fragment anterior...
}

It gives the following error:

Error:(119, 5) error: method does not override or implement a method from a supertype
    
asked by anonymous 07.02.2017 / 21:14

1 answer

3
  

Error: (119, 5) error: method does not override or implement a method from a supertype

The onBackPressed() method does not exist in the Fragment class, so it can not be overridden.

If you want to be able to return to the previous fragment, via the back key, call the FragmentTransaction # addToBackStack (String name) between calls to beginTransaction() and commit() .

FragmentTransaction ft = getFragmentManager().beginTransaction();
...
...
...
ft.addToBackStack(null);
ft.commit();
    
07.02.2017 / 21:58