I need to reload an activity from another activity in android application. That 2nd activity is a dialog window, and based on the dialog window base activity should refresh the contents. (By click button of dialog activity)
Reload your activity.
context.startActivity(context.getIntent());
context.finish();
Consider A is an activity you want to reload and B is another activity.
In this case, just call finish()
when you are moving from A to B. When you call A from B it will load the activity A again.
Dialog Window and activity is completely deferent things.
It is possible to refresh First Activity from it's dialog. You can do it using interface.
Here is simple solution. In your First Activity implements a Interface name IRefreshInteface. It's definition is like bellow:
public interface IRefreshInteface(){
public void doRefreshValue(String commandValue);
}
Now if you implement IRefreshInteface in your activity you will get method doRefreshValue(String commandValue) and write refresh code here.
#Override
void doRefreshValue(String commandValue){
// Write refresh code here
}
Now in your dialog you have Context of your Activity. Using that context object you can call this doRefreshValue(String commandValue) method easily. Here is the sample code :
public AlertDialog displayMessage(Context context, String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
LayoutInflater inflater = LayoutInflater.from(context);
final View v = inflater.inflate(R.layout.custom_view, null);
builder.setView(v);
shipText = (EditText)v.findViewById(R.id.shipNameEditText);
scientistNameText = (EditText)v.findViewById(R.id.scientistEditText);
scientistEmailText = (EditText)v.findViewById(R.id.emailEditText);
volumeText = (EditText)v.findViewById(R.id.volumeEditText);
colourText = (EditText)v.findViewById(R.id.colourEditText);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((IRefreshInteface) context).doRefreshValue("YOUR_COMMAND");
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog));
return dialog;
}
here, pass Context of Activity todisplayMessage() method and call activity method doRefreshValue using this way:
((IRefreshInteface) context).doRefreshValue("YOUR_COMMAND");
For more info visit here and here
Hope this will solve your problem. Sorry for bad english :)
Related
I'm building an Android application and that aplication have an open part and some actions that need authentication.
What I do is to when the user click on an action that needs authentication a login dialog is show, the user can login and the login will be valid for 30 minutes.
So I have 3 buttons in an fragment that when clicked check if the user is logged in, an if not calls a login dialog.
The problem is: How can I identify what button was clicked, because when the user click the button and is not authenticated I just create the login dialog and set the setTargetFragment to the caller fragment, that was the same for the 3 buttons.
Is there a way to pass a parameter to the login dialog to identify that?
The code are as following:
When the user click the button:
final DialogFragment loginDialog = new LoginDialogFragment();
loginDialog.setTargetFragment(this, 0);
btnAprovar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!LoginService.getInstance().isLoggedOn()) {
loginDialog.show(getFragmentManager(), "Login");
}
}
});
And In the dialog:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.fragment_login_dialog, null);
final EditText txtUser = (EditText) v.findViewById(R.id.txtUsername);
final EditText txtPwd = (EditText) v.findViewById(R.id.txtPassword);
builder.setView(v)
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
DoLogin dl = new DoLogin();
dl.execute(txtUser.getText().toString(), txtPwd.getText().toString());
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
private void DoPost(Boolean _result) {
if (_result.equals(true)) {
((PublicacaoDetailFragment) this.getTargetFragment()).onAuthTrue();
} else {
((PublicacaoDetailFragment) this.getTargetFragment()).onAuthFalse();
}
}
What is the best Practice to do that?
Thanks
To extend #Rodolfo's answer, you should probably use the decorator/wrapper pattern here.
Instead of extending DialogFragment, in your LoginDialogFragment you should just have an instance variable of a DialogFragment. This way, you can have a method called show(String buttonDescription), for example. Inside your own show(...) method, you can call the show() method of the DialogFragment.
Since you are extending DialogFragment in a class of your own, you have two choices:
Define a variable (with getter and/or setter) inside your custom Dialog class. In your login button onClick method, before calling show() of your Dialog, call the setter for this variable and pass v.getId(). In this case, v is the View associated with the component that you've added the OnClickListener.
Pass your button View id within a Bundle to your DialogFragment.
I have a little problem with a Dialog.
It's a ListView of Videos with thumbnails that load the videos with an Adapter. The ListView register an OnItemClickListener and inside the OnClickItem method I try to raise the Dialog.
I've tried with various types of Dialog but nothing happened. A simplified piece of code it's here:
public class ListOfVideos extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_videos);
init_phone_video_grid();
}
private void init_phone_video_grid() {
// Here's some code for the video reading
// The ListView
videolist = (ListView) findViewById(R.id.PhoneVideoList);
videolist.setAdapter(new VideoAdapter(getApplicationContext()));
videolist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Here's some code for the video reading
/** ============= Here's the problem ================ **/
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setMessage("Example Message")
.setTitle("This is the title!!")
.setCancelable(false)
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
System.out.println("[debug]" + "Open File " + filename);
}
});
}
The list of videos load perfectly. But when I click on an Item:
The Dialog does not show
I got a error message in the LogCat, that state: "show() Dialog's window is null!"
The println debug message, appears ok in the LogCat
I have searched for that message error, but there's not much information.
I think the problem could be on the Context that receive the Builder, but I'm stuck on this point.
Any advice will be apreciated
That error message is saying that the Context given to the AlertDialog.Builder has no attached window, which Dialogs need as a UI anchor, basically. An Activity is what should be used for such a Context, as it will have the required window.
Without seeing VideoAdapter's code, the root cause is presumably new VideoAdapter(getApplicationContext()), which is handing your VideoAdapter the application Context to build Views with. That likely means that the v passed into onItemClick() is one such View, and v.getContext() is returning that application Context in new AlertDialog.Builder(v.getContext()).
That application Context does not have a window but your Activity does, as mentioned. Furthermore, the Activity is actually what you want to give to VideoAdapter to build Views with anyway, to ensure that they are created with the correct theme and styling. Change that relevant line to:
videolist.setAdapter(new VideoAdapter(ListOfVideos.this));
That alone might solve the issue, depending on what VideoAdapter does internally. However, it's arguably better to specify the Activity again in the AlertDialog.Builder constructor call, just so there's no question:
AlertDialog.Builder builder = new AlertDialog.Builder(ListOfVideos.this);
As a final note, whenever a Context is needed for any UI component, you usually want to use the immediately available Activity.
Here is a example of How to create dialog box..
String message = "Hello";
AlertDialog.Builder alt_bld = new AlertDialog.Builder(
CurrentActi.this);
alt_bld.setTitle("Alert")
.setMessage(message)
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//here right the code that you want perform onClick
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
alert.setTitle("Alert");
alert.show();
May be it will help you..
I have a subclass of ListActivity that is reused throughout my project, subclassed again so that each reports to a different ActivityGroup. This is working beautifully until I try to present a dialog in the superclass (as this behaviour should be exhibited by each subclass), and I get the following error:
ERROR/AndroidRuntime(31514): FATAL EXCEPTION: main
ERROR/AndroidRuntime(31514): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Now, I think this is due to the context that is being fed into the AlertDialog.Builder. I am using standard sample code from Google. Any ideas appreciated.
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
The problem here is that the activity is part of an ActivityGroup. You cannot open a dialog with a context of the current activity in the ActivityGroup, instead provide the context of the ActivityGroup itself.
In the Activity from which you want to present the dialog:
ProgressDialog dialog = new ProgressDialog(MyActivityGroup.group);
In the ActivityGroup subclass, MyActivityGroup, define your static reference:
public static MediaMenuGroup group;
then
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
group = this;
...
I'm using and ArrayAdapter to populate a ListView. After selecting and item, it displays a confirmation Y/N dialog. If the user's choice is negative, then he should be able to select another item showing the same dialog. And so on.
Here's my code:
lView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {
Toast.makeText("Selected file"+ mFiles.get(index).getfileName(),
Toast.LENGTH_SHORT).show();
SelectedFile = mFiles.get(index);
showDialog(DIALOG_CONFIRMIMPORT_ID);
}
});
The weird thing is that while the "Toast" shows the clicked item every time, only the first selected item since the Activity is initiated is being passed to "SelectedFile". No matter how many times you click a diferent item, "SelectedFile" always assumes the same value, the value of the first clicked item, outside of this code.
Heres's my Dialog code:
Protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_CONFIRMIMPORT_ID:
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String message = String.format(getString(R.string.importstudentfileconfirm),SelectedFile.getfileName());
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Activity.this.finish();
// startActivity(new Intent(Activity.this, LOL.class));
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SelectedFile = null;
dismissDialog(DIALOG_CONFIRMIMPORT_ID);
mFiles.notifyAll();
}
});
AlertDialog alert = builder.create();
return alert;
}
}
return null;
}
Thank you very much for any help!
I'm guessing this has something to do with the fact that the onCreateDialog method is only called the first time the dialog is created. So the first time you see the dialog it will have the correct filename.
After onCreateDialog is called, onPrepareDialog(...) is called. onPrepareDialog, allows you to change the dialog after it has been created, but before it gets displayed.
Remember that underneath everything, Android isn't creating a new Dialog for you every time you want to show the DIALOG_CONFIRMIMPORT_ID dialog. It is too computationally expensive to instantiate a new dialog every time. Instead, it creates it once, which causes onCreatDialog to be called, followed by the onPrepareDialog. Every other time the dialog is shown, it only calls onPrepareDialog.
Check out the following article on the Android Developer site. It explains things pretty clearly.
http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
So try using onCreateDialog just for initialization of stuff that won't change between showings of the dialog, then use the onPrepareDialog method to dynamically update the contents of the dialog (i.e. getting the new filename)
Cheers!
I am using a dialog as a form for the user to input data. When they click the "OK" button, the dialog is closed, and I need to use the data that was entered. How can I reference this data in the activity once the dialog has closed?
Get it when the user presses the "OK" button
final EditText input = new EditText(this); // This could also come from an xml resource, in which case you would use findViewById() to access the input
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
mItem.setValue(value); // mItem is a member variable in your Activity
dialog.dismiss();
}
});
Assign an OnDismissListener to the dialog and pass the data to the activity there.
Alternatively, you can create a dialog activity and return the data as the activity result. See the following link for more info about starting activities and gettings results:
http://developer.android.com/reference/android/app/Activity.html#StartingActivities