1

I have created custom dialog and I want to close it on Cancel button click. I searched on google, most of the people are using Dialog or AlertDialog but I am not using anything like that. This is my TextDialogActivity which is loading on button click in my app. From MainActivity I am just rendering another activity as custom dialog. When I click Save button on the dialog I want to access data in parent activity, which is stored in a variable textData in child activity.

  public class TextDialogActivity extends AppCompatActivity {
  TabHost tabHost;
  private static final int FILE_SELECT_CODE = 0;
  private String textData;

  private Button browse;
  private Button cancel_button1;
  private Button cancel_button2;
  private TextView text_preview;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.text_dialog_layout);

      browse = findViewById(R.id.browse_file_button);
      text_preview = findViewById(R.id.text_preview);
      cancel_button1 = findViewById(R.id.cancel_button);
      cancel_button2 = findViewById(R.id.cancel_button2);

      tabHost = findViewById(R.id.tabHost);
      tabHost.setup();

      TabHost.TabSpec spec=tabHost.newTabSpec("tag1");

      spec.setContent(R.id.encode_dialog_text_tab);
      spec.setIndicator("Edit Text");
      tabHost.addTab(spec);

      spec=tabHost.newTabSpec("tag2");
      spec.setContent(R.id.encode_dialog_browse_tab);
      spec.setIndicator("Browse");
      tabHost.addTab(spec);

      browse.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              showFileChooser();
          }
      });

      cancel_button1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
                // close dialog
          }
      });

      cancel_button2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
                 // close dialog
          }
      });

   }
 }

I added @style/Theme.AppCompat.Dialog to my AndroidManifest.xml to make my dialog.

 <activity
        android:name=".activity.TextDialogActivity"
        android:theme="@style/Theme.AppCompat.Dialog"
        android:label="Secret Message">
 </activity>

This is the screenshot.

Jai Prak
  • 2,855
  • 4
  • 29
  • 37

3 Answers3

1

You should treat this as any other activity. Just use intent to send your data from popup activity to parent activity.

You should use the data Intent in onActivityResult(int requestCode, int resultCode, Intent data) like this in the popup:

saveButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

        Intent intent = new Intent();
        intent.putStringArrayListExtra("popup_data", data);
        setResult(RESULT_OK, intent);
        finish();
    }
});  

And in parent:

if (resultCode == RESULT_OK) { 
    ArrayList<String> popup_data= 
    data.getExtras().getStringArrayList("popup_data");
}
Banana
  • 2,435
  • 7
  • 34
  • 60
  • I want to access data from the popup to parent activity, not parent to popup. – Jai Prak Jan 29 '18 at 13:25
  • It is very similar. Use intent to send your data from popup to parent activity, then in the parent activity use the onActivityResult: `public void onActivityResult(int requestCode, int resultCode, Intent data) ` – Banana Jan 29 '18 at 13:32
  • @JaiPrak Glad it helped :) – Banana Jan 30 '18 at 07:58
0

As mentioned above, Activity is not the best to do that.

But just start your Dialog as an Activity for result.

Then when the user presses a button return the result and finish the Activity.

Then in the main Activity you get the result in onActivityResult

See an example here or here

Start the Activity (in MainActivity)

 Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, REQUEST_CODE);

Set the result (in the Dialog)

ExampleClickAdapter clickAdapter = new ExampleClickAdapter(yourObjects);
clickAdapter.setOnEntryClickListener(new ExampleClickAdapter.OnEntryClickListener() {
    @Override
    public void onEntryClick(View view, int position) {
        Intent intent = new Intent();
        intent.putExtra("pos", position);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
});

Receive the result (in MainActivity)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}
Eselfar
  • 3,759
  • 3
  • 23
  • 43
-1

My custom dialog/popup is an activity itself. I am not using Dialog or AlertDialog. For closing this dialog on Cancel button click, this is what I did:

 cancel_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();  // Call this method to close the dialog
        }
 });

And for accessing data from child activity(TextDialogActivity) to parent(MainActivity) activity, I did the same as @Kemo suggested here in answers. This is the code:

// From Parent activity(MainActivity) opening popup on button click
popUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getContext(), TextDialogActivity.class);
            startActivityForResult(intent, TEXTFILE);
        }
});

// From Child activity(TextDialogActivity) sending data to parent activity
save_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!secretText.isEmpty()) {
              Intent intent = new Intent();
              intent.putExtra("popup_data", secretText);
              setResult(RESULT_OK, intent);
              finish();
            }
        }
});

Now I want the result from child activity to parent activity for that I did this in onActivityResult method:

// In parent activity get data on onActivityResult
if (requestCode == TEXTFILE && resultCode == getActivity().RESULT_OK){
     secretText = data.getExtras().getString("popup_data", "");
}
Jai Prak
  • 2,855
  • 4
  • 29
  • 37