0

After many search I can not resolve my problem. I start a dialog from adapter then from my dialog i call an activity (by intent). I would like to return to my dialog with the result from my activity. here my code :

    final Dialog dialog = new Dialog(MyActivity.context);
    dialog_actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            add_dialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {Intent add = new Intent(MyActivity.context,MySecondActivity.class);
                    MyActivity.context.startActivity(add);

From the called activity I would like to return to the dialog. How can i do ? Thanks for help.

Sandy2626
  • 41
  • 2
  • This would be simpler if, instead of a `Dialog`, you used a dialog-themed `Activity`. – CommonsWare Apr 20 '17 at 21:46
  • What do you mean by dialog-themed Activity ? I don't know to use this. – Sandy2626 Apr 20 '17 at 21:51
  • I mean an activity whose theme extends from a dialog-based theme (e.g., `Theme.Material.Dialog`). Visually, it appears like a dialog. However, from a programming standpoint, it is an `Activity`, and so things like `startActivityForResult()` work normally. – CommonsWare Apr 20 '17 at 22:13
  • Thanks but it is not what i want. I have a custom Dialog I would like to keep it. Is there a more adapted answer ? – Sandy2626 Apr 20 '17 at 22:28
  • Finally I have called a dialog-themed in my custom dialog. It's working fine. Thank you CommonsWare – Sandy2626 Apr 23 '17 at 16:47

2 Answers2

0

Instead of showing your dialog from your adapter you can show it from an activity like below:

In your adapter:

((YourActivity)mContext).showCustomDialog();
In your YourActivity.java
public void showCustomDialog(){
    Intent add = new Intent(MyActivity.context,MySecondActivity.class);
    startActivityForResult(add);
}

In your activity handle your result and make changes in dialog.

Let me know if it works for you.

Patrick R
  • 6,621
  • 1
  • 24
  • 27
0

You can use a DialogFragment and override onActivityResult callback to get result.

Note

  • To get the result in your fragment make sure you call startActivityForResult(intent,reqCode) instead of getActivity().startActivityForResult(intent,reqCode) in your fragment.
  • If the hosting activity already overrides the onActivityResult, make sure it calls super.onActivityResult for unhandled result codes.
VishnuSP
  • 599
  • 5
  • 16