Home > Mobile >  Change background on an activity from another activity
Change background on an activity from another activity

Time:05-16

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.

CodePudding user response:

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.

  • Related