There are two buttons, button A starts another activity inside its onClickListener using simple Activity.startActivity() method, button B does some other work inside its onClickListener.
When I click button B and immediately after button A, then new activity is started, onPause() lifecycle event for old activity is fired, but also onClick event for second button is fired, but after Activity.onPause() which leads to some state inconsistencies in my app.
Is there any way to prevent touch/click events from being delivered after onPause() without using isPaused flag?
**Edit:**My code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.activity_button);
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TestActivity.class));
}
});
Button buttonB = (Button) findViewById(R.id.log_button);
buttonB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("TEST", "onClick");
}
});
}
#Override
protected void onPause() {
super.onPause();
Log.e("TEST", "onPause");
}
}
Basically if you are fast enough and click button B right after button A then B's onClick is fired after onPause.
In OnClickListener of button A, disable the button b.
Button.setEnabled(false);
Just enable the button at the of A's onClickListener or at onResume depending on your requirements.
I know this question is very old and far far away from being active. I came across this question via a blog.
A simple solution which I can think off is maintaining a flag globally in Activity A and setting it immediately inside onClick of Button A. This flag can be reset in onResume of Activity A.
This flag should be used at the top of onClick handler and all the clicks of all the views must be ignored. Of course, this requires that there is a single onClick method.
private boolean isOnClickIgnored= false;
#Override
public void onResume() {
super.onResume();
isOnClickIgnored = false;
}
#Override
public void onClick(View v) {
super.onClick(v);
if(isOnClickIgnored){
return;
}
switch (v.getId()){
case R.id.btnA:
isOnClickIgnored = true;
// Do button a things like opening Activity B
break;
case R.id.btnB:
// Do button b things
break;
case R.id.btnZ:
// Do button z things
break;
}
}
This whole concept can be easily solved using ViewModels + lifecycle-aware LiveDatas where LiveData expose events to UI-layer only when it's allowed.
Related
I am aware that a method onBackPressed() exists in java to handle code when back button is pressed.
But I want to know if there is any method or variable which tells me the number of time the back button is pressed?
int counter;
#Override
public void onBackPressed() {
super.onBackPressed(); //remove this line if you don't want to finish the activity on back press
counter++;
System.out.println("Back button pressed for" + counter + "times.");
}
Create a counter variable and increment it in the onClick()
myButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
counter++;
myText.setText(“You click the Button “+count+” times now”);
}}
I have MainActivity which has a cardView inside a recyclerView and I want to double click on the card view to open the SecondActivity But when I touch the cardView twice very quickly I see that the app completely closes and I see android home screen and apps screen But when I touch the app launcher icon again I see the second activity.
(I have handled twice click with a boolean flag)
MyCode:
recyclerView onClick event:
#Override
public void onClick(final View view) {
if (AppActivity.isResponding()) {
Log.i("MainActivity", "onClick: applied");
Intent intent = new Intent(MainActivity.this, ProductGroupDetailActivity.class);
intent.putExtra(ProductGroupDetailActivity.EXTRA_PRODUCT_GROUP_NUMBER, (Integer) view.getTag(R.id.tag_productGroupNumber));
MainActivity.this.startActivity(intent);
AppActivity.setResponding(false);
}
}
SecondActivity:
#Override
protected void onResume() {
super.onResume();
responding = true;
}
what's the problem?
thank you
I have a method which on button click needs to perform in a specific way and if no button is clicked, it should alternatively act differently.
Basically on clicking on the button, it should destroy my service. And if I don't click on it, it will eventually be destroyed. The thing is that I want the destroy method act differently based on these 2 scenarios.
You could add the clicked View as method argument:
myOnClick(View view){
if(view != null){
//Button was clicked
....
}else{
//No Button was clicked
.....
}
}
Is the method performing periodically? Then just add a bool to see if the button was clicked before.
like:
Main:
boolean clicked = false;
onCreate:
Button b = (Button)findViewById(R.id.buttonName);
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(clicked){
clicked = false;
}else{
clicked = true;
}
});
}
as title says, how to make my ripple effect on ImageButton (ripple effect assigned to this ImageButton) activated on backpressed() without back to the previous activity
with a bit of logic you could achieve that easily. what i thought of is that you have a method lets call it doSomething();
boolean onBackpressed = false;
private void doSomthing() {
if(onBackpressed){
finish();
}else{
// do anything else that the button wants to do;
}
}
now onBackpressed() you could do this
#Override public void onBackpressed() {
onBackpressed = true;
myImageButton.performClick(); // myImageButton.callOnClick()
}
P.S: that i didn't call super.onBackpressed(); so we can control the back press without existing the app.
of course your imageButton click listener calls doSomething() method.
There are a lot of options on how to define a click/tap on the touchscreen. One of them for example is setting a boolean.
Example for boolean:
boolean buttonClicked = true;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (buttonClicked) {
//do that and this
}
}
});
And there's a isPressed() method:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (button.isPressed()) {
//do that and this
}
}
});
What exactly is the difference between them? And when and why do I use boolean and the method isPressed()?
Because you are referring to a button in both of your examples, I assume that you are referring to the user tapping on a button, not just a random touch on the screen.
That being said, both of the examples you provided are not good.
In your first example, the boolean is useless because it is always true, so //do that and this will always be reached.
In your second example, your if statement is useless, because the onClick method by its nature is only reached when the button is tapped.
A good way to listen for a button press is using a click listener like this:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Code placed here will run every time the button is tapped
}
});
...where R.id.buttonId is the ID of your button in the layout.
If you need to define click event for a View you can use onClickListener, onTouchListener.
For more information check for Android official Documentation.
onTouchListener
onTouchListener
When considering your first code snippet, You can use boolean to perform another operation on button click event. as example something like this ,
boolean buttonClicked = false;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//true after button clicked
buttonClicked = true;
}
});
//if buttonClicked equals true
if (buttonClicked){
//perform operation only after button clicked
}
when considering your second code snippet, no need of button.isPressed() inside
button's onClick() callback. Because what you want to do by checking button.isPressed() is done without it inside button's onClick() callback.
Keep in mind these things.
isPressed() is a public method of View Class
Button is a subclass of View Class
isPressed() is a public method of Button Class as well.
About isPressed() from Android official documentation.
Indicates whether the view is currently in pressed state. Unless
setPressed(boolean) is explicitly called, only clickable views can
enter the pressed state.
Returns true if the view is currently pressed, false otherwise.