How to implement the method onBackPressed() in a class it inherits from Fragment, Causing it to return to the previous Fragment?

Asked

Viewed 224 times

-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...
}

Gives the following error:

Error:(119, 5) error: method does not override or implement a method from a supertype
  • What was it? Because I was denied?

  • The chunk of code has no implementation, so the error is meaningless to analyze the problem.

  • 1

    @diegofm Whatever the content of the method there will always be this error.

1 answer

3


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

The method onBackPressed() does not exist in the class Fragment, so it cannot be overwritten(override).

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

FragmentTransaction ft = getFragmentManager().beginTransaction();
...
...
...
ft.addToBackStack(null);
ft.commit();
  • Awwww I get it... I thought of implementing some interface but the problem is that I have no idea of the name of the interface to implement this class.

  • but come here ramaral there I Seto null in that parameter is?

  • I’ll read the documentation...

  • Tomorrow I’ll be there and signal you =D

  • 1

    In this situation you can pass any value. A specific value is only necessary if you want to reference that back state in the future.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.