How to use Appcompatactivity functions within a Fragment?

Asked

Viewed 174 times

1

I have an Fragment and need within it some Appcompatactivity functions, as I implement both in the same class?

It’s like this:

public class clientes extends Fragment {

I’ve tried to:

public class clientes extends Fragment implementes AppCompatActivity{

Or vise versa.

if it is not possible to do, as I do this check:

ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {

getSystemService turns red, and you can’t use it in Fragment Besides, I need to use the:

 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = 
  act.getLayoutInflater().inflate(R.layout.lista_curso_personalizada, 
  parent, false);

getLayoutInflater() is also in red, it shows that it is not possible to use it in a Fragment

1 answer

2


getSystemService is a method of Context. Activity is a Context, so you can use to call directly when you’re inside a Activity. Fragment is not a Context, then you need to pick up an object Context and call the getSystemService. The easiest way is:

getActivity().getSystemService(Context.CONNECTIVITY_SERVICE).
  • vlw man, thank you so much

Browser other questions tagged

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