Как из первого активити изменить цвет фона второго активити?
ConstraintLayout lay = (ConstraintLayout) findViewById(R.id.main);
Switch darktema = (Switch)findViewById(R.id.darktema);
Intent intent = new Intent(this, AddActivity.class);
if (darktema.isChecked()) {
int backgroundColor = ContextCompat.getColor(this, R.color.dark);
lay.setBackgroundColor(backgroundColor);
}else{
int backgroundColor = ContextCompat.getColor(this, R.color.thiscolor);
lay.setBackgroundColor(backgroundColor);
}
этот код изменяет цвет оновного активити. Как изменить и цвет второго активити тоже? Если что id его ConstraintLayout - "add".
Ответы (2 шт):
Вы можете отправить значение переменной backgroundColor через Intent.
Примерный код в первой Activity:
Intent intent = new Intent(this, AddActivity.class);
if (darktema.isChecked()) {
int backgroundColor = ContextCompat.getColor(this, R.color.dark);
lay.setBackgroundColor(backgroundColor);
intent.putExtra("backgroundColor", backgroundColor);
} else {
int backgroundColor = ContextCompat.getColor(this, R.color.thiscolor);
lay.setBackgroundColor(backgroundColor);
intent.putExtra("backgroundColor", backgroundColor);
};
startActivity(intent);
Примерный код в AddActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
View yourRootView = findViewById(R.id.your_root_view);
Bundle extras = getIntent().getExtras();
int backgroundColor = intent.getInt("backgroundColor");
yourRootView.setBackgroundColor(backgroundColor);
};
UPDATE
Если AddActivity уже создано, то изменить тему в нём можно, например, с помощью Singleton.
Примерный Singleton:
class YourSingletonClass {
Context addActivityContext = null;
private YourSingletonClass () {
};
protected class Instance {
private final static YourSingletonClass instance = new YourSingletonClass();
};
public static YourSingletonClass getInstance() {
return Instance.instance;
};
public void setAddActivityContext(Context addActivityContext) {
this.addActivityContext = addActivityContext;
};
public Context getAddActivityContext() {
return this.addActivityContext;
};
}
Тогда в AddActivity:
YourSingletonClass.getInstance().setAddActivityContext(AddActivity.this);
Соответственно, из первой Activity после этого можно будет изменить фон так:
Intent intent = new Intent(this, AddActivity.class);
if (darktema.isChecked()) {
int backgroundColor = ContextCompat.getColor(this, R.color.dark);
lay.setBackgroundColor(backgroundColor);
} else {
int backgroundColor = ContextCompat.getColor(this, R.color.thiscolor);
lay.setBackgroundColor(backgroundColor);
};
((AddActivity) YourSingletonClass.getInstance().getAddActivityContext()).yourRootView.setBackgroundColor(backgroundColor);
yourRootView должен быть объявлен вне методов .
Используя интент можно передать информацию о темной теме. При переходе на другую активность передаете данные:
Intent intent = new Intent(this, AddActivity.class);
intent.putExtra("dark", darktema.isChecked());
startActivity(intent);
и в второй активности обработать приходящие данные:
intent.getBooleanExtra("dark", false);
так же ваш код можно немного улучшить:
lay.setBackgroundColor(ContextCompat.getColor(this,if (darktema.isChecked()) {
R.color.dark
}else{
R.color.thiscolor
}));
я думаю так будет красивее :) В активности AddActivity.class можно сделать такую обработку данных о темной теме:
int color = if(ContextCompat.getColor(this,intent.getBooleanExtra("sample", false){
R.color.dark
}else{
R.color.thiscolor
}));