Avoid an item from Action Bar from being double clicked - android

I have designed an action bar for my Android app. In this action bar there's a button that launches a Dialog Activity used to configure my app's behavior. If I double click this button fast enough, I'm able to order the Dialog Activity to be launched twice before it actually appears, and then it appears duplicated and visually overlapped and I don't want this. I tried to create some sort of lock-down mechanism but it is not working because my Dialog Activity is launched only after all the code in my Main Activity calling method (onOptionsItemSelected) is executed. Is there a way to avoid this form happening?
My code is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//ensure only one element from the option menu is launched at once (if you double click fast you could launch two)
Log.e("test", "onOptionsItemSelected ");
if(optionItemAlreadySelected == false)
{
optionItemAlreadySelected = true;
int id = item.getItemId();
if (id == R.id.action_sound_mode) {
//item.setVisible(false);
Intent intent = new Intent(this, SoundConfigurationActivity.class);
startActivity(intent);
optionItemAlreadySelected = false; //this code is executed before the activity is started!!!
return true;
}
}
return super.onOptionsItemSelected(item);
}
Is there a way to know when the Dialog Activity has already being closed and lock the opportunity to open it once again until then.

Kotlin
It's a screen(Activity, Fragment) based solution to avoid a double tap on menu action.
Add below global variable to your activity/fragment containing onOptionsItemSelected function.
private var previousClickTimeMillis = 0L
Write below function anywhere in the project i.e Utils.
fun singleSafeClick(
previousClickTimeMillis: Long,
block: (previousClickTimeMillis: Long) -> Unit) {
val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis < previousClickTimeMillis || currentTimeMillis >= previousClickTimeMillis + OnSingleClickListener.DELAY_MILLIS) {
block(currentTimeMillis)
}
}
Write your triggering code as below.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_delete -> {
singleSafeClick(previousClickTimeMillis) { tappedTime ->
previousClickTimeMillis = tappedTime
// Write Yyur code here
}
}
}
}

You can use a boolean variable to track the state of your Dialog. When you click the button you set mDialogShown = true to block any other show dialog requests.
Now when the user presses Back button and the Dialog is closed onActivityResult is called.
At this point your are sure that the Dialog was closed.
I assumed your code is inside an Activity:
class MainActivity extend Activity {
static final int SHOW_DIALOG_REQUEST = 1; // The request code
static boolean mDialogShown = false; // True if dialog is currently shown
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_sound_mode) {
showDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void showDialog() {
if (!mDialogShown) {
mDialogShown = true;
Intent intent = new Intent(this, SoundConfigurationActivity.class);
startActivityForResult(intent, SHOW_DIALOG_REQUEST);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == SHOW_DIALOG_REQUEST) {
mDialogShown = false;
}
}
}
Documentation
https://developer.android.com/training/basics/intents/result.html
https://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog

Related

Popupmenu is closing when screen rotated

I am using a popupmenu like below;
popupMenu = PopupMenu(view.context,view);
val menuInflater:MenuInflater = popupMenu!!.menuInflater;
menuInflater.inflate(R.menu.profil_image_degistir_menu,popupMenu!!.menu);
this.profilImage = profilImage as ImageView;
popupMenu!!.setOnMenuItemClickListener(object:PopupMenu.OnMenuItemClickListener{
override fun onMenuItemClick(item: MenuItem?): Boolean {
if(item?.itemId == R.id.action_cam){
camContext = view.context;
kameraIzin = ContextCompat.checkSelfPermission(view.context,Manifest.permission.CAMERA);
if(kameraIzin != PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE);
}else{
val intent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_REQUEST_CODE);
}
}else if(item?.itemId == R.id.action_localStorage){
}
return true;
}
});
popupMenu!!.show();
But when I rotate the screen menu is closing? How to prevent this?Can u help me.
When you rotate the device a configuration change takes place; this causes the activity to be recreated again. To control configuration changes you should use the onSaveInstanceState method. This method execute when a configuration change occur. In this case you should save a boolean value indicating if your popup is open or not.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("shouldShowPopup", isPopupOpen);
}
Then, on your onCreate method, you should check if the Bundle param is null. If is not null you can get the value we have saved before and show (or not) the popup.
In manifest file you need to add android:configChanges="orientation" in the activity that hosts this popup menu.

Android application does not relaunch when navigating to the home screen

We have a launcher application that works fine on older versions of Android. We have a device that is running Android 5.1, and are running into issues.
When pressing the back button from within the application, we allow the user to go to the settings page. Pressing the home key re-launches the application. Pressing the back button on other devices also relaunches the application.
On the new device, pressing the back button allows us to navigate to the Android home page. It does not launch the application.
We are overriding the back button like so:
#override
public void onBackPressed() {
// Display the password prompt if required
if (PreferencesManager.isPasswordPresent()) {
LeaveApplicationPasswordDialogFragment dialog = LeaveApplicationPasswordDialogFragment.getInstance();
dialog.show(getSupportFragmentManager(), "password");
}
else {
// Prompt whether we are about to leave the app
LeaveApplicationDialogFragment dialog = null;
MyApplication application = (MyApplication )
getApplication();
if (application.isDefaultLauncher()) {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ** to access the device's settings?");
}
else {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ***");
}
dialog.show(getFragmentManager(), "leaving");
}
}
In the dialog fragment, we accept the confirmation and process it like so:
public void exitToSettings() {
GUIAndroidTouchBaseActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
shutdownOperations();
finish();
}
Per some research and other threads, I worked with our exit method like so:
public void exitToSettings() {
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
GUIAndroidTouchBaseActivity.this.startActivity(intent);
shutdownOperations();
finish();
}
No dice. Same behavior.
What am I missing? Is there something in OS 5.1 that's overriding our launcher? Again, pressing the home button launches the app as expected. Navigating to the home page from the settings page by pressing the back button does not.
What we have works on other devices and OSs. We've had no issue with 4.1 and 6.1.
We are also overriding the back button like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent KEvent) {
int deviceid = KEvent.getDeviceId();
//Making sure not processing same key again
if (KEvent.getRepeatCount() != 0) {
return true;
}
if (!SettingsOpened) {
int keyaction = KEvent.getAction();
// "Esc" key can not be stooped id diveceid is non zero because it can be back key of android
if (KEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && deviceid != 0) {
return super.onKeyDown(keyCode, KEvent);
}
if (keyaction == KeyEvent.ACTION_DOWN) {
String key = KeyEvent.keyCodeToString(keyCode); //wont work in version 11 or less
if (keyCode != KeyEvent.KEYCODE_ENVELOPE) {
Matcher matcher = KEYCODE_PATTERN.matcher(key);
if (matcher.matches() || ExternalKeyboard.keyMatches(KEvent)) {
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState());
char character = (char) keyunicode;
//toast.makeText(this, "onKeyDown" + _lastChar + repeatcount, toast.LENGTH_SHORT).show();
_lastChar = character;
_actionDown = true;
ExternalKeyboard.KeyboardAddChar(character);
}
}
}
return true;
}
else {
return super.onKeyDown(keyCode, KEvent);
}
}
Thanks!
Adding
android:stateNotNeeded="true"
android:clearTaskOnLaunch="false"
to my manifest took care of it.

Disable Back button in WizarDroid

I am using WizarDroid library, and creating a simple Wizard.
What I am trying to do is at the final step I want to disable Back button at the bottom. I have searched every where and the documentation is not helping me.
Here is my Basic Wizard code:
public class FormWizard extends BasicWizardLayout
{
public FormWizard()
{
super();
}
#Override
public WizardFlow onSetup()
{
return new WizardFlow.Builder()
/*
* Mark this step as 'required', preventing the user from advancing to the
* next step without selecting one option.
*/
.addStep(Form1.class, true)
.addStep(Form2.class, true)
.addStep(Form3.class)
.create();
}
#Override
public void onWizardComplete()
{
super.onWizardComplete();
// Terminate the wizard
getActivity().finish();
}
}
The Form1,Form2 and Form3 are simply extending WizardStep and showing some data to user.
In the documentation the wizard.goNext(); method is defined but it is not available in my scenario.
I simply want to disable the user to go back after they reach at Form3 or final step.
If you are talking about disabling the software/hardware (next to the home button and menu button) then you simply have to handle the onPress yourself. Example:
//FOR API 5-
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "Can't go back", Toast.LENGTH_LONG).show();
}
//FOR API 6+
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getApplicationContext(), "Can't go back", Toast.LENGTH_LONG).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}

read a nfc chip after the users clicks a button

is it possible to start reading from a nfc-chip after pressing a button. After pushing the button there should be a message like "please hold your nfc chip close to the device..". I only see tutorials, which show how to start the application when holding a nfc chip to the device (onNewIntent).
Second Question. What if the application is already running and i hold the nfc chip next to my device? Is it forcing a destroy and then launches again?
Thanks!
Regarding the first part of your question, you could use a flag within your activity that indicates the state of your application (ready to write/message is showing, not ready to write/message not showing). YOu can find a simple example here:
private static final int DIALOG_WRITE = 1;
private boolean mWrite = false; // write state
public void onCreate(Bundle savedInstanceState) {
[...]
// Set action for "Write to tag..." button:
mMyWriteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switch to write mode:
mWrite = true;
// Show a dialog when we are ready to write:
showDialog(DIALOG_WRITE);
}
});
[...]
}
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DIALOG_WRITE:
// A dialog that we show when we are ready to write to a tag:
return new AlertDialog.Builder(this)
.setTitle("Write to tag...")
.setMessage("Touch tag to start writing.")
.setCancelable(true)
.setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int arg) {
d.cancel();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface d) {
mWrite = false;
}
}).create();
}
return null;
}
// You would call this method from onCreate/onStart/onResume/onNewIntent
// or from whereever you want to process an incoming intent
private void resolveIntent(Intent data, boolean foregroundDispatch) {
String action = data.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
// The reference to the tag that invoked us is passed as a parameter (intent extra EXTRA_TAG)
Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (mWrite) {
// We want to write
mWrite = false;
// TODO: write to tag
} else {
// not in write-mode
// TODO: read tag or do nothing
}
}
}
Regarding the second part of your question, when you want to receive NFC tag discovery events while your activity is already in the foreground, you should register with the NFC foreground dispatch system. See Advanced NFC: Using the NFC Foreground Dispatch System. There is no need to destroy and re-create your activity.

onActivityResult never called for speech Intent on Google Glass

I have a menu Activity with several options. One of them is to post to a news feed using Glass's speech-to-text. I looked at https://developers.google.com/glass/develop/gdk/input/voice#starting_the_speech_recognition_activity
and have it all implemented, but the onActivityResult method apparently never gets called.
On the Glass device, I'm able to select "New Post" in the menu and the voice capture comes up. I can speak to it and it'll convert my speech to text on the screen. But after I accept it (by tapping or waiting a few seconds), it just exits and takes me back to the home screen. I need to be able to get the speech text String in onActivityResult and call another method (displayPostMenu) to show another menu to process the text but I can't do that if onActivityResult is never called.
I've looked at several similar issues but none of the solutions have worked/were applicable... I don't think I can setResult() on RecognizerIntent.ACTION_RECOGNIZE_SPEECH since it's Google's? Any help would be greatly appreciated!
Some pieces of my code:
private final int SPEECH_REQUEST = 1;
//Code to make this Activity work and the menu open...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.view_feed:
//Stuff
return true;
case R.id.new_post:
Log.i("MainMenu", "Selected new_post");
displaySpeechRecognizer();
Log.i("MainMenu", "Ran displaySpeechRecog under new_post selection");
return true;
case R.id.stop:
Activity parent = getParent();
Log.i("MainMenu", "Closing activity; parent: " + parent + "; " + hashCode());
if (parent != null && parent.getApplication() == getApplication()) {
finish();
} else {
MainMenu.close();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
// Nothing else to do, closing the Activity.
finish();
}
public void displaySpeechRecognizer() {
Log.i("MainMenu", "Entered displaySpeechRecognizer");
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(speechIntent, SPEECH_REQUEST);
Log.i("MainMenu", "Finished displaySpeechRecognizer. startActivityForResult called.");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("MainMenu", "onActivityResult entered from MainMenu");
switch (requestCode) {
case SPEECH_REQUEST:
Log.i("MainMenu", "onActivityResult enters SPEECH_REQUEST case");
if (resultCode == RESULT_OK) {
Log.i("MainMenu", "onActivityResult enters RESULT_OK for voice cap");
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
Log.i("MainMenu", "SpokenText:" + spokenText);
holdText = spokenText;
if (holdText != "") {
displayPostMenu();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
You say this is a "menu activity", so does that mean that it's attached to a live card?
If so, are you overriding onOptionsMenuClosed and calling finish inside it?
If you are, the menu activity will finish and destroy itself before the speech activity returns, so there will be no place for the result to go back to.
One approach to fix this would be to use a flag to indicate whether you should defer the call to finish when the menu is closed, and make that call conditionally inside onOptionsMenuClosed based on that flag. Then, set that flag in your displaySpeechRecognizer method and wait until the end of your onActivityResult processing to call finish instead.
Something like this should work (written without testing, may contain typos):
private boolean shouldFinishOnMenuClose;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// By default, finish the activity when the menu is closed.
shouldFinishOnMenuClose = true;
// ... the rest of your code
}
private void displaySpeechRecognizer() {
// Clear the flag so that the activity isn't finished when the menu is
// closed because it will close when the speech recognizer appears and
// there won't be an activity to send the result back to.
shouldFinishOnMenuClose = false;
// ... the rest of your code
}
#Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed();
if (shouldFinishOnMenuClose) {
finish();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST) {
if (resultCode == RESULT_OK) {
// process the speech
}
// *Now* it's safe to finish the activity. Note that we do this
// whether the resultCode is OK or something else (so the menu
// activity goes away even if the user swipes down to cancel
// the speech recognizer).
finish();
}
}

Categories

Resources