-1

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
kAsdh
  • 356
  • 1
  • 10
  • 25

3 Answers3

1

Reload your activity.

context.startActivity(context.getIntent());
context.finish();
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
DroidNinja
  • 997
  • 1
  • 8
  • 9
0

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.

Plochie
  • 3,944
  • 1
  • 17
  • 33
0

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 :)

Community
  • 1
  • 1
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87