Question: How do we call a method located in another activity? For example I'm trying to use this method from the MainActivity public void change

How do we call a method located in another activity? 

 

For example I'm trying to use this method from the "MainActivity"

public void change () {
    ConstraintLayout layout = findViewById(R.id.main_layout);
    layout.setBackgroundResource(R.drawable.wallpaper);

}

into the second activity (SecondActivity) when clicking a button

I tried this 

MainAcitvity MA = new MainActivity();

MA.change();

 

but it didn't work. what's the correct way to do it in Android? 


Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

In Android you should not instantiate an Activity using new as you did in your example as Activities should be managed by the Android system itself an... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!