How To Check If My Button Is Clicked In Another Activity?
Solution 1:
You could set different set some variable to different values(eg. if you click on button1 make its value as 1 or else 2) pass that integer variable as an intent to the previous activity and use GetExtras() to retrieve the integer variable...now use the IF condition to determine which button was clicked
publicvoidonClick(View view){
int code;
switch(view.getId()){
case R.id.button1:
code=1;
break;
case R.id.button2:
code=2;
break;
}
Intent i = newIntent(this, ResultActivity.class);
i.putExtra("yourcode", code);
startActivityForResult(i, REQUEST_CODE);
}
Now back to your old activity use
getIntent().getExtras("yourcode").toString(); to retrieve it
Solution 2:
If you have an
Activity
A(which has the buttons),Activity
B and you are starting B from A: You can pass your selection at A with bundle while starting B:Intent intent = newIntent(A.this, B.class); intent.putExtra("selection_of_button_at_A", selectionOfButtonAtA); startActivity(intent);
and get that value at B's
onCreate
method:getIntent().getStringExtra("selection_of_button_at_A");// sample for string
But if your B is not started from A, then you may store your selection at SharedPreferences(when a selection is done at A):
SharedPreferencesprefs=A.this.getSharedPreferences("com.your_app", Context.MODE_PRIVATE); prefs.edit().putString("selection_of_button_at_A", selectionOfButtonAtA).commit();
now you can reach the state of your button clicks anywhere on your application.
SharedPreferencesprefs= B.this.getSharedPreferences("com.your_app", Context.MODE_PRIVATE); StringselectionOfButtonAtA= prefs.getString("selection_of_button_at_A");
Solution 3:
If you just need to get the button of which you clicked on activity1 to get you to activity2 I would suggest you to do as follows: for Activity1 as the current activity and Activity2 as the calling activity.
declare in Activity2
public static final String EXTRA_IS_BUTTON_ONE = "isButtonOne"
. the use of a constant value allows you to guarentee the same String will be used thus making your code more readable and make less room for bugs. besides, saving some cycles according to Android Performance Guidelinescreate a new Intent to transfer to activity2 in activity 1->
Intent intent = new Intent(Activity1.this,Activity2.class);
create boolean is button one to put in the intent declaring which button clicked (button1 clicked --> true, button2 clicked --> false) ->
intent.putExtra(Activity2.EXTRA_IS_BUTTON_ONE,isButtonOne);
in Activity2 receive the intent and get the boolean ->
boolean isButtonOne = getIntent().getBooleanExtra(EXTRA_IS_BUTTON_ONE,false);
(the false is the default case if you didn't pass the extra to the activity).in Activity2 you may use the boolean to decide which text to
Hope I helped, feel free to ask questions if something ain't clear.
Solution 4:
Use this. It refers to fragments but it's the same for activities. Basically you create an interface in one activity and call it in the other. As simple as that.
Solution 5:
In simple words:
Use startActivityForResult
method.
In explicit explanation:
A launches B for result.
B handles click event and set the result, the result will goes back to A.
A receives the result.
For more detailed explanation and even example code snippet, please refer to Android Developers which provides a quite good example.
Post a Comment for "How To Check If My Button Is Clicked In Another Activity?"