Вы можете отправить значение переменной 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
должен быть объявлен вне методов .