I am developing small android application and in my application I am using fragment concept. In my one activity I am using multiple fragments and doing transitions of those fragment.
Now Inside one fragment I am displaying one alert dialog and for i need to pass context of activity. I did this in this following manner.
new AlertDialog.Builder(getSherlockActivity())
.setTitle("Logout")
.setMessage("Really Logout from App?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).setNegativeButton("No", null).show();
Everything is working fine without any error and without any failure. Only one problem is that in logcat it showing me that my main activity has leaked intent.
Here is the problem; is this problem because I am passing getsherlockActivity(); or because of some thing else?
Wanted to clarify this concept ...
Excuse me, but do you sure this message is related to this part of code, because i really don't know how Activity Fragment and Dialog can refer to Intent?
It looks like info level message related to BroadcastReceiver, not to this part of code. Here is couple of lins related to this problem: LINK and LINK
Related
Curiosity question here.
I use a lot of dialogs builders and most of the time my negative cancel button do nothing except dismiss the dialog. The code I found everywhere on the web is this :
builder.setNegativeButton(
"cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
I happened to find out that this code do exactly the same :
builder.setNegativeButton("cancel", null);
So my question is then : is that a bad habit not to manually dismiss the dialog, and if yes why ?
It is somewhat documented behaviour, see:
http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.
So with null listener you exercise this implicit documented behaviour of Dialog.
What can go possibly wrong? (yeah, I think there's at least 50% chance that some custom ROM out there is not behaving properly... then again, who cares about custom ROMs failing to follow documented behavior, I don't any more, too much of that BS).
I learned this myself in an Android course in school. Basically, you only need to implement the button listener if you need additional functionality.
So it is not "habit" to include the click listener, it is just clear intent.
I'm new to Android, only three days mostly studying the basics. I ended up with this code while studying the creation of an android alert dialog:
OnClickListener oclBtnOk = new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
}
});
alertDialog.show();
}
};
But I still have somethings to get known:
1- First time I made the code I was using AlertDialog alertDialog = new AlertDialog.Builder(this).create(); instead AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();. The first code wans't compiling, so, what exactly is the difference? That means the dialog builder is son or has some dependency with the MaiActivity? That means the listener from this dialog is inside the main activity?
2- Is this really the correct way to create a simple alert dialog? 'cause in my console I saw one red line saying "ActivityManager: Warning: Activity not started, its current task has been brought to the front" and also studying through http://developer.android.com/guide/topics/ui/dialogs.html what they recommend is to use AlertDialogFragment as container of my dialog..
And my last question is a little bit more complicated but a simple answer as "Yes" to lead me I a deeper research would help- android has some sort of EDT (Event Dispatch Thread) since is based on java? I mean, to process graphics (like progress bars) should I separate them into another thread?
AlertDialog.Builder requires a Context. The code is in an OnClickListener anonymous subclass and it is not a Context. this refers to the subclass instance. To refer to the this of the parent activity class, it's scoped as MainActivity.this, and activities are Contexts.
It's all right at this point of your learning curve. The "Warning: Activity not started, its current task has been brought to the front" is nothing to worry about - the app was already running and was just brought to front, not re-launched.
Android does not run AWT and there's no EDT by that name. However, the main thread (also called the UI thread) does something similar.
To answer your first question:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
The code is running in an anonymous class, so this does not reference an object derived from Context. Adding the MainActivity. makes the argument reference an Activity which is derived from Context
Your second question: Is this really the correct way....?
It is certainly one acceptable way. There are others.
The message you are seeing about bringing the activity to the front is not related to dialogs. You may eliminate it by exiting from your application before starting a new debug session.
I haven't written any code for this yet, but I've been researching how to implement bookmarks in my custom web browser. From what I've read, I believe the way to go is to show the user a dialog (I saw this article on how to return the value from the dialog) containing, I think, a ListView for the bookmarks... I'm honestly stuck at something pretty simple - how to present the bookmarks to the user and select one.
So, where my questions:
what's "best practice" for displaying a list to the user and having
him select one?
is doing this in a Dialog "best practice"?
Thanks.
The classic way to do this (pre 3.0) is to use simple Dialogs, that you manage with your current Activity. The easiest way to go is to use the AlertDialogBuilder to build the dialog, see here, around the middle, the "Adding a list" section. This way you get a Dialog with a list, and the user can select exactly one entry from that list.
Nowadays however, you should be using DialogFragments, with the (not so) new Fragment framework. You can use the official compatibility lib to make fragments work on older Android builds. In a DialogFragment, you can either show any UI layout you want if you override the onCreateView(...) callback, or you can define the looks and behavior by using the "onCreateDialog(...)" callback (you can use AlertDialogBuilder here, too). See the link for examples.
The DialogFragment based solution is more self-contained and you can easily call/show it from any place in your Application.
And yes, I do think that a single-select list-based dialog can be considered the "best practice" in this kind of situation. However, the other advantage of the DialogFragment based solution is, that you're not forced to show it in Dialog-style, you can also embed it into an Activity's layout as a standard fragment if that's what you want.
Best is the to show a dialog with a list and upon user selection navigate to either browser or webview.
you can use the below code to present a dialog to user::
String[] yourarraylist = new String[]{"A","B","C","D","E","F","G"};
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle("title");
builder.setSingleChoiceItems(yourarraylist, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), yourarraylist[item], Toast.LENGTH_SHORT).show();
//launch web browser or webview
alert.dismiss();
}
});
alert = builder.create();
alert.show();
you can launch web browser as below::
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Also for opening the url in webview you can refer my blog at this LINK
I would personally use another activity to display the bookmarks rather than using a dialog. The user could have a large number of bookmarks saved and a dialog I don't think would be the best way.
I would make another activty which extends ListView and display the bookmarks on a list or even better a gridview with the thumbnail of the bookmark. Then by clicking a bookmark will return to the main activity refreshing the browser with the selected bookmark.
No need for dialogs, unless you like to create a longpressclick or context menu to show a dialog for deleting/editing/.. bookmarks.
The alertdialog.builder is the quickest and easiest way to build dialog. As another tip to build dialogs with alertdialog.builder. The builder has a setview to give it any view you want to the dialog, this view can basically be a linearLayout with lost more view already in it creating a complex dialog view.
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle(getResources().getString(R.string.dialog_title));
ad.setView(dialogLayout);
I have an app that shows a welcome screen via an alert dialog. I use the following code in the onCreate method of the Activity:
wsBuilder = new AlertDialog.Builder(this);
wsBuilder.setIcon(android.R.drawable.ic_dialog_alert);
wsBuilder.setTitle(R.string.instructions_title);
wsBuilder.setMessage(R.string.welcome_1);
wsBuilder.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
wsBuilder.show();
When I start the app, most of the time the screen darkens like it does when the dialog is going to
display, but the dialog never shows up. The screen just stays darkened and none of the touch events get through. I can click the back button on the phone to dismiss the dialog and then the app works like normal, but I can't figure out why the dialog doesn't fully display. Once in a while the dialog actually displays, but most of the time it doesn't.
Any help in running down this issue would be greatly appreciated.
OnCreate may not be the best place for it as the application is loading try using it onStart
public void onStart()
{
//Your code here
}
Activity would be better for wellcome screen.
I have a class that extends android.app.Dialog, the layout is done in an xml file, and the setup (button listeners, etc) is done on the onCreate method. My problem is that whenever the dialog is displayed, then dismissed, and displayed again, the Editable TextViews are still populated with the information that was displayed previously. What is the common way to clear these text fields? Remember - this is a separate class that extends Dialog - so there is no 'onDialogCreate' like Activity has.
Or, perhaps I am extending the wrong class? There is just a lot of processing being done, and do not want to have all the code in the main Activity. I would like it to be in a separate Class. I tried to extend AlertDialog, but it does not create the border like Dialog does. Any help would be great.
The dialog is shown via the Activity:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_NEW_SAFE:
return(new NewSafeDialog(this));
default:
return(null);
}
}
onCreateDialog(..) caches the dialog which means the same instance is reused.
3 ways to fix the undesired behavior off my head:
Override onPrepareDialog(..), use findViewById(..) to get whatever you want to clear, clear it.
Don't rely on managed dialogs at all, do new NewSafeDialog(this).show() each time you want to show the dialog.
Add onCancelListener(..), onDismissListener(..) inside your custom dialog that would call a method to clear itself.
The good way to create a dialog is by using showDialog() as you did so don't change it.
The good and easy way to force deletion of a dialog in order to make your creation code recalled again is:
void removeDialog (int id)
So if you simply do the following, it's gonna work ;)
removeDialog(DIALOG_NEW_SAFE);
showDialog(DIALOG_NEW_SAFE);
Try clearing the text in the constructor of the NewSafeDialog i.e. your dialog class.