public abstract class BaseActivity extends AppCompatActivity { static final String TAG = "tag"; public final String ACTION = "NIGHT_SWITCH"; protected BroadcastReceiver receiver; protected IntentFilter filter; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setNightMode(); filter = new IntentFilter(); filter.addAction(ACTION); receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive: needRefresh"); needRefresh(); } }; registerReceiver(receiver, filter); } public boolean isNightMode(){ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); return sharedPreferences.getBoolean("nightMode", false); } public void setNightMode(){ if(isNightMode()) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); Log.d(TAG, "setNightMode: " + sharedPreferences.getBoolean("nightMode", false)); this.setTheme(R.style.NightTheme); } else setTheme(R.style.DayTheme); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); } protected abstract void needRefresh(); }把activity改成fragment
时间: 2024-02-15 08:26:35 浏览: 118
好的,将这段代码转换成 Fragment 代码大致的步骤如下:
1. 创建一个新的 BaseFragment 类,继承自 Fragment。
2. 将原来在 BaseActivity 类中的代码复制到新的 BaseFragment 类中。
3. 在 BaseFragment 类中,将 onCreate() 方法改为 onActivityCreated() 方法,并将方法参数改为 savedInstanceState 和 View 类型的布局视图对象。
4. 在 onActivityCreated() 方法中,调用 setNightMode() 方法,用于设置当前主题。
5. 在 BaseFragment 类中,将 isNightMode() 方法中的 getBaseContext() 方法替换为 getContext() 方法。
6. 将原来在 BaseActivity 类中的 needRefresh() 抽象方法添加到 BaseFragment 类中,并实现该方法。
7. 在原本的 Activity 中找到容器布局,改为使用 FragmentTransaction 将新的 BaseFragment 实例添加到容器布局中。
下面是将 BaseActivity 转换为 BaseFragment 的代码示例:
```
public abstract class BaseFragment extends Fragment {
static final String TAG = "tag";
public final String ACTION = "NIGHT_SWITCH";
protected BroadcastReceiver receiver;
protected IntentFilter filter;
@Override
public void onActivityCreated(Bundle savedInstanceState, View view) {
super.onActivityCreated(savedInstanceState, view);
setNightMode();
filter = new IntentFilter();
filter.addAction(ACTION);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: needRefresh");
needRefresh();
}
};
getContext().registerReceiver(receiver, filter);
}
public boolean isNightMode(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
return sharedPreferences.getBoolean("nightMode", false);
}
public void setNightMode(){
if(isNightMode()) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
Log.d(TAG, "setNightMode: " + sharedPreferences.getBoolean("nightMode", false));
getActivity().setTheme(R.style.NightTheme);
}
else getActivity().setTheme(R.style.DayTheme);
}
@Override
public void onDestroy() {
super.onDestroy();
getContext().unregisterReceiver(receiver);
}
protected abstract void needRefresh();
}
```
希望这个例子对您有所帮助。
阅读全文
相关推荐


















