user2850914
user2850914

Reputation: 21

How to make reference to a button in another activity?

I don't know how to make a reference to a button that is in another activity. For example: I'd like to change a status of a button, that belong to activity A and change the status the button on activity B. Thanks!

Upvotes: 0

Views: 1097

Answers (2)

ayon
ayon

Reputation: 2180

You can't do this directly. If you seriously need that then when you switch from Activity1 to Activity2 through an Intent , just pass a value to Activity2 that related to the status of the button in Activity2 you are trying to achieve. Then, in Activity2 onCreate method , retrieve that value and set the status of that button accordingly. Say you want to make Button invisible , then in Activity1 , use you can do like this

Intent intent = new Intent(Activity1.this , Activity2.class);
intent.putExtra("buttonStatus" , "invisible");
startActivity(intent);

Then in Activity2 oncreate method ,

String value = getIntent().getExtras().getString("buttonStatus");
if(value.equals("invisible")){
  MyButton.setVisibility(View.INVISIBLE);
}

Hope it helps.

Upvotes: 0

John Zeringue
John Zeringue

Reputation: 725

My understanding of Android is that you don't. If you need to change the state of one activity from another, you either pass some sort of signal through an intent or change some persistent value (maybe part of a database).

Upvotes: 3

Related Questions