I am developing an application in Android Studio with Firebase where I am using the map to show different things. The first time the user enters the MapsActivity a alertdialog pops up with some information. My problem is that when the "OK" button is pressed the application automatically duplicates the activity and the duplicated activity is ran. When i press the back button I can go back to the "original" activity again. I think it is because of the multiple Firebase calls. Are there any fix to this?
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(m1)) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MapsActivity.this);
dialog.setTitle("Bus stop");
dialog.setMessage("Do you want your kid to get picked up here?");
dialog.setNegativeButton("Cancel", null);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
database.child("Addresses").child(m1.getTitle()).child("name").setValue(m1.getTitle());
database.child("Addresses").child(m1.getTitle()).child("time").setValue(m1.getSnippet());
database.child("Addresses").child(m1.getTitle()).child("users").child(userId).setValue(username);
database.child("User").child(userId).child("address").setValue(m1.getTitle());
}
});
dialog.show();
}
Related
I have an app that needs to use location services for one of its features. So when the user clicks on a button, I would like my app to check to see whether the device has location services enabled, and if it doesn't prompt the user to enable it via an AlertDialog. When the user clicks the 'Enable Location Services' button, I want the app to take the user to the settings page where the user can enable it.
My problem is that when the user clicks the 'Enable Location Services'
button, instead of opening up the settings page, the app goes straight
into the main activity.
Note: This is all happening inside of a fragment whose parent activity is my second Activity. So here is the layout of my app: MainActivity (login page) => Second Activity (parent activity) => fragment (where this check must occur).
Here is my code inside of the fragment's onCreateView method:
currentLoc.setOnClickListener(new View.OnClickListener() {
DataActivity dataActivity = (DataActivity) getActivity();
Context context = getActivity().getApplicationContext();
#Override
public void onClick(View view) {
if (!dataActivity.isLocationServiceEnabled()){
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("To use this feature, you must first enable location services");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Enable Location Services", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}});
alertDialog.show();
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("GPS Enabled...");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
});
Note: currentLoc is an imageView that is created inside the
fragment_layout file
More information:
compileSdkVersion 26
buildToolsVersion "25.0.3"
minSdkVersion 14
targetSdkVersion 26
compile 'com.android.support:appcompat-v7:26+'
compile 'com.android.support:support-v4:26+'
I have tried countless variations that all use intents and for this project they all take me back to the login page (MainActivity). When I create a new project and use the same code, it works...
I am asking the same question which is asked before at below links but the solution proposed in these links is not working for me so I am posting it again.
How to make an AlertDialog disappear?
Android AlertDialog always exits when I click on OK Button
How to navigate to next activity after user clicks on AlertDialog OK button?
Basically, I am creating an AlertDialog builder to notify the user for asking to enable a setting for the Usage Data Access and when the OK button is pressed then the Settings menu gets opened. When I press back button to come back on the app then the AlertDialog is still available there although I expected to be dismissed to be back on my app.
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
builder.show();
return;
}
Any hint what wrong could be going here?
Edit after some testing:
I tested OPs code on 6.0.1 and it behaved as expected - i.e. the dialog was dismissed after clicked 'OK'. I'll leave my initial answer below as an alternative that also works. Additional alternatives can be found here.
You can get a reference to your Alert Dialog it from your builder.show() method:
mMyDialog = builder.show();
In your onClick method:
mMyDialog.dismiss();
Full sample:
AlertDialog mMyDialog; // declare AlertDialog
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
mMyDialog.dismiss(); // dismiss AlertDialog
}
});
mMyDialog = builder.show(); // assign AlertDialog
return;
}
When I login to my android application for the first time, its works perfectly, after I logout, when I try to login its doesn't open the activity,
I have cleared FragmentStack and saved preferences below.
clearFragmentStack();
clearSavedPrefs();
But When i disable or destroy the application from recent apps or delete data from settings. its working normally.
How can I solve this issue. Please find the code below.
builder.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
httpClientService.canCanLogout(new ResponseCallBack<BaseDao>() {
#Override
public void onDataReceived(
HttpResponseHolder<BaseDao> responseHolder) {
if (responseHolder.getErrReason() != null) {
generalProperListener.showShortToastMessage(responseHolder.getErrReason());
} else {
startUserLoginFragment();
ct.event.push("Logout");
}
}
});
}
});
I have cleared the answer.
Where I have concat the previous string value.
//old
UserAddress.ADDR_URL +="?".concat(paramsString);
//new
UserAddress.ADDR_URL =URL+"?".concat(paramsString);
I have a dialog that prompts users to enable gps location if it's not enabled.
After opening the settings, and the user enableing gps, and pressing the back button from the location settings screen they come back to the app, but the dialog is still visible.
Here is the code for the button clicked.
// On pressing Settings button
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
dialog.dismiss();
}
});
My question is why does the dialog.dismiss() not close the dialog, I have also tried dialog.cancel() with same result. Is there something I should be doing after opening the settings screen?
Thanks,
I have exactly this code in my Activity:
private void showLocationDisabledInfo() {
final Context c = this;
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setMessage("TEST");
builder.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
c.startActivity(intent);
}
});
builder.setNeutralButton(R.string.cancel, null);
builder.create().show();
}
and it does close the dialog automatically, no matter what button I click. It's from my application, and it was tested on devices with APIs 8 and 10, and emulator running API 17. The only difference (possibly) between our codes is the value of mContext. Please provide the whole code responsible for setting up the dialog and the environment in which you've seen described behavior.
Can anyone figure out why this causes a force close??
void failbox(){
// Create the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// Set the message to display
alertbox.setMessage(R.string.fail);
alertbox.setPositiveButton("Get Busybox", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=stericson.busybox"));
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
});
// show the alert box
alertbox.show();
}
Thanks in advance!
If you're getting the forced close in the emulator, that's just how it is, as best I can tell. You cannot access the market through the market app from an emulator.
Does your app still crash when run on a real Android device?
(Of course, some folks have figured out sneaky ways to get to the market from an emulator. See How to install Android Market App on the emulator?)