当targetSdk小于19时,在PreferenceActivity的子类中都要重写isValidFragment ,在isValidFragment 方法中进行fragment名的合法性校验
时间: 2024-03-08 21:47:25 浏览: 47
是的,如果应用的`targetSdk`小于19,那么在`PreferenceActivity`的子类中,需要重写`isValidFragment`方法,并在该方法中进行`Fragment`名的合法性校验。这是因为在`targetSdk`小于19时,`PreferenceActivity`是允许加载任意的`Fragment`的,这可能导致一些安全问题。
以下是一个重写`isValidFragment`方法的例子:
```
public static class MyPreferenceActivity extends PreferenceActivity {
@Override
public boolean isValidFragment(String fragmentName) {
// 对Fragment名进行合法性校验,如果合法则返回true,否则返回false
return MyFragment1.class.getName().equals(fragmentName)
|| MyFragment2.class.getName().equals(fragmentName);
}
}
```
在这个例子中,`isValidFragment`方法判断传入的`fragmentName`是否等于`MyFragment1`或`MyFragment2`的类名,如果等于则返回`true`,表示这个`Fragment`是合法的,可以被加载;否则返回`false`,表示这个`Fragment`是非法的,不能被加载。
开发者可以根据自己的应用程序需求,自定义`isValidFragment`方法的实现逻辑,来保证应用程序的安全性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)