Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change background on an activity from another activity

I’m trying to change the background on an activity from another activity but it’s not working.
ConstraintLayout layout = findViewById(R.id.main_layout);
layout.setBackgroundResource(R.drawable.summer);

When I change the main_layout (main activity) to second_layout (second activity) it works on the second activity, but I can’t get it to work on the first activity.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s not possible. The second activity doesn’t even have a reference to the first activity to do so. Unless you’re trying to use statics to keep a reference to the first activity, which is a memory leak, wrong, and will cause lots of problems. If you need to interact like this, you should either call the second activity for a result and return the new behavior in a result the first activity can apply, or write it to shared preferences and let the first activity parse it from there in onResume.

Also, findViewById will only find views in the current Activity. From the point of view of another activity, the UI of the first activity doesn’t exist.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading