S
S
sanke462018-10-15 23:32:01
Android
sanke46, 2018-10-15 23:32:01

How to control View not from Activity (where it is) but from Fragment?

I have a problem, I need to change the background of the object from the main Activity from Fragments how to do it ??
I tried different ways but they don't work:

val view2 = LayoutInflater.from(context).inflate(R.layout.activity_main, null)
                val drawerLayout = ButterKnife.findById<DrawerLayout>(view2, R.id.main_drawer_layout)
                drawerLayout.setBackgroundResource(R.drawable.bg_2)
                ButterKnife.bind(this, view2)

fun changeBackground(firebaseId: String) {
        when(firebaseId) {
            "One" -> main_drawer_layout.setBackgroundResource(R.drawable.bg_grey)
            "Two" -> main_drawer_layout.setBackgroundResource(R.drawable.bg)
            else -> main_drawer_layout.setBackgroundResource(R.drawable.bg_2)
        }
    }

but not one method does not work, it gives an error that Drawable is not init, although I use butterKnife

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-10-15
@sanke46

You have a mistake in understanding how Android works.
Read more about life cycles.
In the meantime, roughly consider that the screen is drawn from scratch each time.
You can't just say that you have a new picture in the background.
You need to enter a shared preference (a setting that will be saved in the file)
And when drawing, set the background in accordance with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question