I have and android application in which I am initializing a third party library in the onCreate() of the main activity. Now when there is any runtime error in initialization of the library, My application will crash and at that point I want to show an AlertDialogue to the user saying "some error message" in the dialogue box.
The problem is that when the application crashes, my alert dialogue is showing up but it is dismissed along with the application. I want it to be persisted even when the application crashes.
Below is the code of alert dialogue which I used.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Error!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
Can anyone guide me in how to make that AlertDialogue independent of the activity, so that it doesn't require the app window. If not possible, then what other widgets, I can use or customize?
If you want your app to keep running, avoid crashing.
If you want special handling for issues in a library init, catch the relevant exceptions and keep the app running in a state that shows the dialog and then does something else, such as finish()ing the activity that had a problem in its init.
To avoid the 3rd party library from taking down your app, wrap its API calls in a try/catch block, e.g.:
try
{
BuggyLibrary.init(); // example
}
catch (Exception e)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Error!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
You'll need to consider the other implications of this library not initializing ... if your app can't continue functioning without it you'll need to gracefully finish() your Activity when the dialog is dismissed.
May be it's possible to achieve something like that.
There is a library that allows you to show activity when your app crashes. Basically it launches new activity in another process.
At the same time you can show activity as a dialog by using Theme.AppCompat.Dialog for your activity as a theme.
Perhaps combining these two approaches will get what you want.
P.S. Or by using old fashioned try-catch as some people suggest.
Related
I'm trying to display a Toast right after I open a Settings Activity, which should contain a few instructions on what to do in the opening screen.
For example, let's say I want the user to connect to a specific network, so in my code I run
Toast.makeText(context, "Instruction message", Toast.LENGTH_SHORT).show()
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
to display a Toast followed by a call to open up the WiFi Settings Activity. The problem here is that the Toast will display in the current Activity inside my app, but very quickly disappear as soon as the new Activity comes up.
I'm wondering if there's a way of displaying that same Toast but on the opening Activity, rather than the one that is doing the call, in order to properly instruct the user on what to do on such new Activity.
Or perhaps there's a better way of achieving this?
I think it would be better if you used a dialog, so after clicking OK, the user will be shown the screen to change WiFi, like so:
AlertDialog.Builder(context)
.setTitle("Your app name")
.setMessage("Please turn on WiFi on the following screen.")
.setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id ->
{
startActivity(Settings.ACTION_WIFI_SETTINGS)
}
})
.setNegativeButton("Cancel", null)
.show();
You can read more about Dialogs here: https://developer.android.com/guide/topics/ui/dialogs
Some services vary between Android and iPhone, for example floating widget, widget on the home screen, can I make them using flutter
Can I view a dialog on the home screen like this example?
update
thank everyone answer, but I need how to get it in Flutter
Yes you can show that
new AlertDialog.Builder(YourActivity.this)
.setTitle("Alert")
.setMessage("Alert Message")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Here you can do whatever you want to do on click
}
}).show();
Sorry you can't do it in iOS, however in Android, you can, all you need to do is write platform specific code.
You could create an Activity with the Theme.Dialog theme. In your AndroidManifest.xml file add the theme to the activity, like this:
<activity android:name=".DialogActivity" android:theme="#android:style/Theme.Dialog"></activity>
From your service simply start this Activity. You will have to start the activity with the Intent.FLAG_ACTIVITY_NEW_TASK flag. See How to start an Activity from a Service
Source
I have almost completed my app development and I have even allowed a way to show up a toast message if there is no internet connection. I need to know how to close an app after showing a dialog if there is no internet connection?
My app is a webview app. So when user turn on the app, the app must check for internet connection. and display a dialog box if no network and the app must close after showing dialog.
OR
I don't want users to see the "webpage not available" page, so if there is no internet connection, the app must close after showing a dialog box with one button 'close'. Or is there any way to redirect users to a text in directory which shows no internet connection?
I need those code only; all the other codes have been done and are working properly.
Just invoke finish() to destroy activity in the onClick() function of dialog. Like:
...
builder.setMessage('There is no connection!! Please close the activity!')
.setPositiveButton('close', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
...
You can popup a dialog after checking the internet connectivity as follows
new AlertDialog.Builder(this)
.setTitle("Connection Error")
.setMessage("You are not connected to Internet.")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// finish the activity here or,
// redirect to another activity
}
})
.show();
You can check your internet connectivity before setting the view to the activity.
Like :
if(connnected){
setContentView(layoutId);
}else{
show a tosat with information no internet;
finish activity.
}
call finish() method on your activity once user clicks on dialogue
I have an AlertDialog with an OK button in my activity by using this code:
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
//Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.icon);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
}
});
alertDialog.show();
This works fine within the application, but I want this to be ran and show even if we're outside of the application. Of course with the application still running and not closed. Much like Toast. even if I can use a Toast with an OK button I'd be happy.
Any help is appreciated. Thanks.
You can't run a dialog as standalone. You will have to get an Activity to display your dialog. In my mind, your solution would lead to a bad user experience if every app could launch a modal dialog whenever it wants.
But you can imagine 2 solutions :
bringing up an activity with this dialog open (maybe you could make the activity transparent) when a certain intent is sent to the system.
use notifications, it looks quite close from what you want to do.
Initial description:
My application handles some events and do some task related to that event, even if application is in background. I am using receivers.
Problem description
I want to show notification when an event starts or end.
What I did yet
I used Notification manager :- But I need to show notification on home screen not on notification bar.
I used App widget :- But user will not able to notice any changes in information on app widget easily, because this does not do any eye catching effect.
I used Toast :- But this having time limitation to show, I want to show notification until user close.
So what should be the option? How can I do my task?
Please refer below image, as what I want
Unfortunately, you've explored all the possible options. What you want to achieve is not provided intentionally because it will make for very bad user experience. Imagine a notification (of the kind that you want) popping up when you are looking at an email!
Attention piracy
When your app is in the foreground, you can do a lot of things.
When your app is not in the foreground, there are only two options
Start an activity: You must do this only if essential (like an incoming phone call activity). If the user deems it more irritating than useful, your app is going down the drain!
Use the notification manager.
An option is to use Alert Dialogs
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
Or you can use run an Activity with the Dialog theme.