解决主题是否改变为活动
问题描述:
我在写一个应该支持gui主题的应用程序。应用和配置它没有问题,但理解上的问题是某个特定活动的主题发生了变化。解决主题是否改变为活动
存在使用几个主题活动。还有另外一个扩展了PreferenceActivity
并提供了选择主题的功能。所选主题的ID保存为共享首选项。当某些使用主题的活动获取onResume()
时,我想检查当前主题ID是否与共享首选项中保存的主题ID相同。但Theme
对象没有任何标识或方法来标识它。
更新:现在我正在考虑在每个活动为当前主题的字符串名称,但这种解决方案看起来比较难看,因为我将不得不相同的变量添加到应用程序中的每个活动。
什么是做这个检查的正确方法是什么?我做错了什么?
答
我已经看到,需要重新启动应用程序的主题更改生效的应用程序。我不认为这是一个问题。这样你就可以在启动时应用这个主题,而不用再担心它了。
答
这可能是你想要的。我正在使用一个标记,以便在设置活动中设置主题并返回到此调用活动后,将重新启动调用活动,并在SharedPreferences
中将主题设置为某个值。
private boolean activityStarted = false;
public void onCreate(Bundle savedInstanceState) {
Log.d(DEBUG_TAG, "onCreate()");
activityStarted = true;
setTheme(); // user-defined method
super.onCreate(savedInstanceState);
...
}
@Override
protected void onResume() {
super.onResume();
Log.d(DEBUG_TAG, "onResume()");
if(activityStarted == true){ // it has come from onCreate()
activityStarted = false; // Set flag to false so that next time onResume()
// is called after resuming existing activity,
// the activity will be restarted and theme will
// be set
} else { // it has directly come to onResume()
finish();
startActivity(getIntent());
}
}
更新
我不知道这是否是这样做的更好的办法,但不是把逻辑到onResume()
方法,我重写onActivityResult()
方法并重新启动Activity
如果先前Activity
是设置Activity
。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.settings) {
Intent settings_intent = new Intent(this, Settings.class);
startActivityForResult(settings_intent, SETTINGS);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
// Identify our request code
switch (reqCode) {
case SETTINGS:
if (resultCode == RESULT_CANCELED) {
Log.d(DEBUG_TAG, "RESULT_CANCELED");
finish();
startActivity(getIntent());
} else if (resultCode == RESULT_OK) {
Log.d(DEBUG_TAG, "RESULT_OK");
}
break;
}
}