I have the below code and the line PreferenceFragment frag = new PreferenceFragment()
returns a warning, which I don't know how to resolve:
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.util.Log;
import android.widget.Toast;
import com.time.myapplication.NotificationCenter;
import com.time.myapplication.R;
import com.time.myapplication.data.DeadlinesUtils;
public class Settings extends PreferenceActivity {
private static final int RESULT_RECOVER_PICK = 1;
private static final String TAG = "Settings";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceFragment frag = new PreferenceFragment(){
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference prefPersis = findPreference(getString(R.string.pref_key_notif_persist));
prefPersist.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String act = (Boolean) newValue
? NotificationCenter.ACTION_SHOW
: NotificationCenter.ACTION_HIDE;
sendBroadcast(new Intent(act));
return true;
}
});
Preference prefToggle = findPreference(getString(R.string.pref_key_notif_toggle));
prefToggle.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
sendBroadcast(new Intent(NotificationCenter.ACTION_TOGGLE));
return true;
}
});
Preference prefBackup = findPreference(getString(R.string.pref_key_backup_do));
prefBackup.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_STREAM, DeadlinesUtils.performBackup(getApplicationContext()));
startActivity(i);
return true;
}
});
Preference prefRecover = findPreference(getString(R.string.pref_key_recover_do));
prefRecover.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
Settings.this.startActivityForResult(i, RESULT_RECOVER_PICK);
return true;
}
});
Preference prefVersion = findPreference(getString(R.string.pref_key_about_version));
try {
PackageInfo pkgInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
prefVersion.setSummary(pkgInfo.versionName);
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, "Failed to retrieve version number", ex); // FIXME translate
}
}
};
getFragmentManager().beginTransaction()
.replace(android.R.id.content, frag)
.commit();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_RECOVER_PICK == requestCode) {
if (RESULT_OK == resultCode)
DeadlinesUtils.performRecover(getApplicationContext(), data.getData());
else
Toast.makeText(this, "Failed to perform recover", Toast.LENGTH_SHORT).show(); // FIXME translate
}
}
}
The warning message is
Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static less... (Ctrl+F1)
Inspection info:From the Fragment documentation: Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().
Note that this is no longer true when you are using androidx.fragment.app.Fragment; with the FragmentFactory you can supply any arguments you want (as of version androidx version 1.1). Issue id: ValidFragment
What am I doing wrong?