开发者

Resolving if theme is changed for the activity

开发者 https://www.devze.com 2023-01-28 04:32 出处:网络
I\'m writing an application that should supports gui themes. There is no problem with applying and configuring it, but there as a problem with understanding is theme changed for a particular activity.

I'm writing an application that should supports gui themes. There is no problem with applying and configuring it, but there as a problem with understanding is theme changed for a particular activity.

There are several activities which use t开发者_如何学编程hemes. And there is another one which extends PreferenceActivity and provides the functionality to choose a theme. The id of choosed theme is saved to shared preference. When some activity which uses themes gets onResume() called I want to check if the current theme id is equal to the one saved in shared preferences. But Theme object doesn't have any id or method to identify it.

UPDATE: Now I'm considering having a string name of current theme in each activity, but this solution looks rather ugly, because I would have to add the same variable to each activity in application.

What is the correct way of doing this check? Am I doing anything wrong?


I have seen apps that require you to restart the app for theme changes to take effect. I don't think that's a problem. That way you can just apply the theme on startup and not worry about it again.


This may be what you want. I am using a flag so that after I set the theme in a settings activity and return to this calling activity, then the calling activity will be restarted and the theme set to some value in 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());
  }
    
}

Update

I'm not sure if this is a better way of doing this but instead of putting logic into the onResume() method, I am overriding the onActivityResult() method and restarting the Activity if the previous Activity was the Settings 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;
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消