Android: Fast button presses results in multiple instances of intent - java

Currently I'm experiencing a bug where if the user quickly taps a button, the intent that the button is attached to will fire off multiple times, resulting in a stack of that intent that will need to be back traced through again. How can I avoid this or remedy this?
Thanks
~k
This is inside of the onClickListener. I set the boolean value here, then I unset it at the end of the process.
if(!isDating)
{
intent.setClass(context, EventDate.class);
isDating = true;
((TabGroupActivity)
context).startChildActivity("EventDate",intent);
}

Try setting the flags for the intent like
intent.setFlags(FLAG_ACTIVITY_BROUGHT_TO_FRONT);
You can also set this flag via AndroidManifest.xml file in the Application section. Prefer this method over above one.
Updating launchMode using the Manifest file
Hope this resolves your issue.

If i had a dime for this bug my QA filed I would be pretty rich if not a millionaire :P There are only so much one can do. Based on your implementation you could try a few things.
As already mentioned, use a boolean. Set it to true once you click it and check this boolean if the button is clicked again. Set it to false once you are done.
Use a progress dialog in case its a long activity like receiving data in your next activity before it displays. This also gives a hint to the user, that something is happening and he need not worry.

use
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it fixes the problem

Actually I found a better solution!
by setting the onClickListener(null); then recreating it onResume, it bypasses having to use flags and what not.
!k

Related

Android: How to enable a setting from a different activity?

I want to toggle a SwitchPreference from another activity (Main Activity).
I have an activity where I ask the user if they want to enable notifications. It contains two buttons and they either click yes or no, as shown below:
I want the answer to be saved and the SwitchPreference to either be toggled, On for yes or off for no. (as well as the SwitchPreference state)
I'm really stuck on how to do this.
Any advice or answers would be most appreciated?
You've got a couple options.
You could do as #0X0nosugar suggested where you simply store the new setting value in SharedPreferences and check the value when resuming the previous activity.
You could use startActivityForResult(intent, SOME_CONSTANT_IDENTIFIER) (vs just startActivity(intent)) in order to have your settings activity return a result to your calling activity. The concept here is that you're starting a new activity which will return a result back to the previous activity. You could then check the value, store it where needed, and update any relevant UI items. Here's an article on how this works https://developer.android.com/training/basics/intents/result.

Android - Check if activity is open for the first time

Before people say this is a duplicate of "check if android is on first run", this question is to check if the activity itself (not the app in whole) is open for the first time.
I have different activities that run Material Tap Target Prompt, so a few pop-ups that explain the buttons and functions.
But I only want it to run for first time users.
Now I tried the following:
if (prefs.getBoolean("firstRun", true)) {
prefs.edit().putBoolean("firstRun",false).apply();
........Do the pop ups
}
But this will set it for the whole app and so when the user gets to the next screen it won't run because the boolean is set to false.
So I am trying to find a way to check if the activity itself is opened for the first time but I can't seem to find anything that would solve this issue.
I thought about using a variable then setting it to 1. But if the users restarts the app, it crashes etc then that var will be reset.
May other option is to create a row in a DB and then check if that is set to 1 or whatever depending on the activity.
But maybe there is an easier way?
Thank you
Why don't you just create preference keys for each Activity. Sample code added below:
if (prefs.getBoolean(MainActivity.class.getCanonicalName(), true)) {
prefs.edit().putBoolean(MainActivity.class.getCanonicalName(),false).apply();
........Do the pop-ups
}

Preventing tilt from calling onCreate, and time measurement of an activity running.

Hi and thanks in advance for your time and attention.
I actually have 2 questions i'm not too sure about building an android app. Pretty basic stuff I believe:
1) My app is fully on Horizontal mode, like AngryBirds for example. When it starts the user figures out he should hold the phone like that, if he isn't already. And that is setup in the manifest for all the activities and works fine. but is there a way to prevent the physical device tilting to call onCreate again? can i override it's method or whatever? the reason i'm asking, is because i have a few ButtonViews that after you click on them, change their picture. i am using onRetainNonConfigurationInstance() to save the array of those ImageButtons, and i even mark the ones changed with the ImageButton setTag() and getTag() methods, so when onCreate is called because of the device tilt it gets the saved array from getLastNonConfigurationInstance() , but i've been trying to make it work for quite some time now and I just can't get it right. After the device tilt (I'm actually using the emulator so it's Ctrl+F11 but i believe it will happen with a device as well) all of the ImageButtons loose their pictures.. Long story short - are there better ways of doing this that you can recommend or is preventing the tilt from doing anything is possible or better?
2) What is the best way to know how many seconds the user has been on a screen? I tried to use two longs that i get via SystemClock.currentThreadTimeMillis() as follows: get the starting time onCreate, and the ending time on the method i call to move to the second intent just before i startActivity. but I think because they are called from different threads, the endingpoint - startingpoint is not correct. What is the way to insure both of the methods of SystemClock.currentThreadTimeMillis() are called from the same thread, the main thread of the activity that i'm stopwatching? Is there a better way to go around this?
Thanks again.
You are doing the right to handle orientation change. Please refer to this link http://developer.android.com/guide/topics/resources/runtime-changes.html . This will help you to get it working.
Good way would be to count the time between onResume and onPause. OnCreate is not called all the time if you are resuming activity.
1) You can try adding the property android:configChanges="orientation" to your activity in the manifest file. It worked for me when my dynamic Action Bar tabs got messed up upon rotation of the screen.
You need specify orientation in android manifest for each of your activities, it will not call onCreate then. android:screenOrientation look at http://developer.android.com/guide/topics/manifest/activity-element.html
User see and interact with your activity starting since onResume and ends on onPause. Other time it does not guarantee that user actually see and can click on something in the activity. System.getCurrentMillis() is good enough.

Way to successfully change Content View?

I have an app that uses this RibbonMenu and I wanted to know if it was possible to use the menu items to change the ContentView of the Activity instead of another activity to reduce the size of my app. I've tried doing this already,and it changed the content view perfectly, but i was not able to press the home button. I'm in a bit of a rush here, but i'll try to post code if anyone asks for it. thank you!
Menu items is UI element. It does nothing by itself. It however can trigger some more actions in your code. As for changing activity layout, yes, you can call setContentView() at any time you want.
to reduce the size of my app
this is not the way optimalizations should be done

Android "Complete action using" set as default check box. Where is the flag that this sets when clicked?

Does anyone know exactly what happens when the "set as default for this action" check box is clicked in the "Complete action using" dialog box. I assume there is a flag set somewhere and I was just trying to figure out where that flag was and if I can set and clear it in code so the dialog box does not keep popping up.
See the answer to Custom filtering of intent chooser based on installed Android package name: this points at the source code for ChooserActivity and ResolverActivity.
Looking in ResolverActivity (see line 197) there is this chunk of code:
getPackageManager().addPreferredActivity(filter, bestMatch, set,
intent.getComponent());
Apparently this lets you tell the package manager what the preferred activity for a specified IntentFilter is. The function onIntentSelected builds up all those variables so you'll need to go running through that function to figure out the best way to make that work for you.
I'm not certain you'll be able to set it globally though: my expectation is that the only way to set that value is from within the chooser package, and since your code won't be running with the privileges of that package I don't think you'll be able to change it.

Categories

Resources