Activity closes when double click a CardView to open - android

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

Related

Android: click event after Activity.onPause()

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.

How to close activity in android?

I have a menu button on the bottom and I have display menu icons from another activity like MenuTask.
But how to close that MenuTask activity if the user clicks again in the menu?
imgMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (key == 0) {
// I want to show menu here
key = 1;
Intent intent = new Intent(MainActivity.this, MenuAction.class);
startActivityForResult(intent, 2);
setResult(0);
} else if (key == 1) {
// I want to Delete menu here
key = 0;
//onBackPressed();
finish();
}
}
});
Note: I think if i click second time , button could not fire. it means else part not execute.
Problem:
I have Clicked, right top menu button and open new activity menu action. but if i want to click again same menu button, i want to disappers that menu action. but menu button could not clicked.
How to make MenuButton Clickable? any idea? any other menthod? But when i click mobile backbutton menuaction layout disappeared.
Thanks in advance.
I thing this is not best way. You must use Fragment
If you want only kill activity MenuAction.this.finish();
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
// handler to delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
you can go though this link Clicking the back button twice to exit an activity
same question

How to dismiss PopupWindow when clicked outside?

In one of my activity, there is a button, which when clicked opens a PopupWindow.
My intention is, when the PopupWindow is opened and user clicks anywhere on screen except the popup area, the PopupWindow should be dismissed.
For this I have set:
// onClick()
myPopupWindow.setOutsideTouchable(true);
myPopupWindow.setFocusable(false);
Issue is it works fine & PopupWindow gets dismissed when I click anywhere outside, but if I click on the button that generated this PopupWindow, then that event is consumed and PopupWindow first gets closed and then gets opened again.
I tried moving my button onclick() code to onTouch().
But if I return true, then button consumes every event & opens popup again and again, even with slightest drag while touching the screen.
If I return false, it behaves same as in onClick() & opens the popup again when button is touches back.
So how can I dismiss the PopupWindow even when clicked on the button?
Just disable that button after it has been clicked so that it can't be clicked when the pop-up window is displaying. button.setEnabled(false);
I solved a similar problem as follows:
settingsImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!settingsMenu.isShowing()) {
settingsMenu.showAsDropDown(settingsImageButton);
settingsImageButton.setEnabled(false);
}
}
});
settingsMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
settingsImageButton.setEnabled(true);
}
}.execute();
}
});
I call the method setEnabled() asynchronously, because onDismiss event is earlier than onClick event.

Finished activity automatically reopens

I have a problem in a very simple application.
I have a main activity, and on button click I open a second activity:
newEntryButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Intent intent = new Intent(MainActivity.this, NewSpendingActivity.class);
MainActivity.this.startActivity(intent);
return true;
}
});
When I close this activity (for example by tapping the back button) or by calling finish() the view loads once again. It only closes if I tap the back button again. What may be the cause of this?
It's probably because you're using an OnTouchListener, which triggers on press, move and release. Try using an OnClickListener instead.
This should not give you problems:
newEntryButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent activityChangeIntent = new Intent(MainActivity.this,NewSpendingActivity.class);
MainActivity.this.startActivity(activityChangeIntent);
}
});

Long Touch handling in Android

I want to open dialog activity on long touch of current activity.
I have accomplished it on simple touch event but i want to perform the same on long touch so that if user is touched the screen by mistake it will not affect on app.
How to acieve it?
Any help is appriciated.
You don't touch an activity but a view that is shown by your activity. You can set a View.OnLongClickListener with the setOnLongClickListener method.
I want to open dialog activity on long touch of current activity.
You can not set the event on click of an Activity you can set the event on the Particular layout or a view(the root layout is better if you want to set it for whole activity).
you can set event on LongClick on view or layout by using following line.
yourViewOrLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//The Action you want to perform
return false;
}
});
hope this helps.
Try this :
RelativeLayout rl=(RelativeLayout) findViewById(R.id.relative1);
rl.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return false; // do false here
}
});
EDIT :
Intent loginIntent = new Intent(ypurActivity.this, Login.class);

Categories

Resources