Displaying alert dialog only after a specific activity page - android

I have a login page.When the user logs in correctly,I want to show an alert dialog saying that your login details have been verified.Click continue to proceed.Now I want this alert dialog to be shown on the next activity page after login.
Here is the alert dialog code-:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean dialogShown = settings.getBoolean("dialogShown", false);
if (!dialogShown) {
AlertDialog.Builder builder = new Builder(Myperwallet.this);
builder.setTitle("Fast Cashier!");
builder.setMessage("Logging In");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
//continue activity here....
}
});
builder.create().show();
// AlertDialog code here
}
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dialogShown", true);
editor.commit();
I tried this but now alert dialog won't display after I have logged out.I need to know how can I set the flag to true when I have pressed the logout button.Here is my logout button functionality-:
logout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Myperwallet.this).create();
alertDialog.setTitle("FastCashier:");
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent logout = new Intent(Myperwallet.this,Login.class);
startActivity(logout);
}
});
alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});

You need to add some implementation to your current code.
You can keep a flag in sharedPrefs and initially it would be true. After showing it for first time make it false.
This is what you must have implemented right now.
So to show it again after logout reset the flag to true when user has logged out. So again when u reach to the activity the dialog would be shown.

actually could not get Under stood more issue, but you want show your AlertDialog in some Optionnale Requirement such as when Login then it show and in next page is Required but due to SharedPreferences displayed only once at the start of the application. When I logged out and then logged in I did not get any alert dialog
SharedPreferences what do do is store your specific Sting with key and you need to match with it. after loing suceessufully you need to Update SharedPreferences with some new String and need to match with it.
also You can use File to check isExit() or not and write require Sting it and Read it and match with you require condition to show alert dialog. this may Helps you.

keep track of the activity flow ,i.e when u start your application it is doing what so ever is in onCreate method ,for ur problem set flag in the on create method ,and when u switch from any other activity to this one implement the method on Restart() and clear ur previous data ....
like this...
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
//ur code
}
hope it will be helpful,

Related

Android: How to make AlertDialog disappear on clicking ok button?

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;
}

Video call got disconnect due to do not keep activities check in android device

ongoing video call got disconnected when it goes to on pause because of do not keep activities checked under developer option in android device how can a user stay in video instead of that option is checked
You can check "Is the do not keep activities is checked or not" by using below line,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
If it returns one, create a AlertDialog and ask the user to disable it first, if user clicks on positive button then create an intent for the Settings like below,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
if (value == 1)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage("You need to disable `do not keep activities` option, press YES")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I am entirely not sure if I got you correctly.
But does this: https://developer.android.com/training/scheduling/wakelock.html
answer your question?
It prevents your app from entering onPause, when a voice-chat is ongoing.

Show dialog and restart an Activity getting LeakedWindow error

I have a language setting in my android app. I save the current language into SharedPreferences and I have a onSharedPreferencesChanged that restarts all activites in the stack so they can be shown in the correct language.
This works like a charm but the language setting is being shown into a Dialog, so when I click on the language I want to change it to, shared preferences changes and then I get a Leaked Window error.
public void showDialog(View v) {
final CharSequence[] items = { res.getString(R.string.english),
res.getString(R.string.spanish) };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(res.getString(R.string.change_language));
builder.setSingleChoiceItems(items, selected,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // English
if (selected == 0) {
Toast.makeText(
SettingsActivity.this,
res.getString(R.string.current_language),
Toast.LENGTH_LONG).show();
break;
}
changeLocale("en");
break;
case 1: // Spanish
if (selected == 1) {
Toast.makeText(
SettingsActivity.this,
res.getString(R.string.current_language),
Toast.LENGTH_LONG).show();
break;
}
changeLocale("es");
break;
}
alert.dismiss();
}
});
alert = builder.create();
builder.show();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("language")) {
restartActivity();
}
}
This method is in the parent activity of all of my activities:
public void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Im getting a leaked window in the line builder.show() because I'm restarting all the activities when the shared preferences change. How can I show the dialog and restart my activities without getting this error?
Thanks in advance
Explaining Abdallah's answer try to move dismiss() up as the first statement in your onClick callback method.
EDIT: First of all after you call alert = builder.create(); you should call alert.show() and not builder.show(). Furthermore having the modifications mentioned in comments you should not receive the error no more.
Your code is trying to show a Dialog after exiting the Activity.
To prevent this, you should dismiss() your Dialog before restarting Activities.

DialogResult in normal java class and return result

I am working on an android project where I am trying to show a AlertDialog in a separate normal java class and return the result that the user enters. I can display the dialog fine but the problem I am having is it always returns the value before the dialog has had one of the buttons pressed.
Below is the code that calls the function in the standard java class to show the dialog
private void showDiagreeError()
{
Common common = new Common(this);
boolean dialogResult = common.showYesNoDialog();
Toast.makeText(getApplicationContext(), "Result: " + dialogResult, Toast.LENGTH_LONG).show();
}
And below is the code that shows the actual dialogue
public boolean showYesNoDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you do not want to agree to the terms, if you choose not to, you cannot use Boardies Password Manager")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = false;
}
});
AlertDialog alert = builder.create();
alert.show();
return dialogResult;
}
dialogResult is a global variable visible throughout the class and being set to false. As soon as the dialog is shown the toast message is shown showing the result is false, but I was expecting the return statement to block until the user has pressed one of the buttons too set the variable to the correct value.
How can I get this to work.
After many hours hunting through the inner depths of google pages, I found this Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style).
It does exactly the job I was after and tested to make sure there are no ANR errors, which there isn't

how to set different message on application startup in android?

I am creating one application for student. I need to set the different message whenever
user open application.I don't understand how to do this or which method use for this.
I search lot of articles but i didn't found anything.
So please provide me some reference or code.
You can store your messages using any storage like sqlite,file or sharedprefrences and retrive message randomly on app opning..
Save your messages in a persistent storage. In android, you could use SharedPreference http://developer.android.com/reference/android/content/SharedPreferences.html, or a Sqlite databse http://developer.android.com/reference/android/database/sqlite/package-summary.html depending on your specific need. Store the messages in either of them and read back a different message each time.
Store some msgs in a SharedPreference at some point in your Activity:
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Editor ed =pref.edit();
ed.putString("0","msg0");
ed.putString("1","msg1");
ed.putString("2","msg2");
ed.putString("3","msg3");
ed.commit();
Then in onCreate(), retrieve a random sg and diplay to the user:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Random r = new Random();
String msg = pref.getString(r.nextInt(4)+"", "none");
Toast.makeText(this, msg, Toast.LENGTH_LONG ).show();
}
You should read the about the fundamentals of android, this would guide you on how to do this. You're not going to spout garbage at the user, there will be a pattern. Once you find the pattern then you turn that logic into java.
Use AlertDialog
Code example below taken from:
http://www.mkyong.com/android/android-alert-dialog-example/
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder.setMessage("Click yes to exit!").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();

Categories

Resources