szpic szpic - 1 month ago 10
Android Question

Should an internal DialogFragment class be static or not?

This is a snippet of code from my project that I am using to learn Android:

private void enableLocationSettings() {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}

@SuppressLint("ValidFragment")
public class EnableGpsDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Tytuł")
.setMessage("wiadomosc")
.setPositiveButton("odpal", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
enableLocationSettings();
}

})
.create();
}
}


As you can see I must add
@SuppressLint
to make my app work but on the guide this annotation wasn't necessary.

What am I doing wrong?

Here are my imports:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

Answer

The example doesn't have those annotations because the class is in its own file. This means it is independent of the Activity that uses the Fragment.

In your case, your Fragment is inside an Activity and doesn't use the static modifier. This means it is tied to the Activity instance.

Having the Fragment depend on the Activity instance is a bad idea, both these classes have complex lifecyles (especially since Activities get destroyed and recreated quite often) and should be independent of each other.

You will need to make EnableGpsDialogFragment's modifier static.

public static class EnableGpsDialogFragment extends DialogFragment {

A static class does not depend on the enclosing class' instance, so the warning will go away.

For more information, read the Java tutorial on nested classes.

Edit in response to your edit: Now that the classes don't depend on each other's instance, you will have to get out an instance of YourActivity so you can call enabledLocationSettings() one way is by casting and will only work if EnableGpsDialogFragment is only used by YourActivity:

@Override
public void onClick(DialogInterface dialog, int which) {
  enableLocationSettings();
}

to

@Override
public void onClick(DialogInterface dialog, int which) {
  ((YourActivity)getActivity()).enableLocationSettings();
}

If this Fragment will be used by multiple Activities, you should create an interface to be implemented by each Activity instead.