0

I have an activity that contains a series of elements displaying data (Date, time, number, description). When I select one of the elements, a new activity starts and the previous data from the selected element populates the fields of the new activity. If the fields were blank then new data can be entered.

Selecting the Done button returns to the main activity and populates the relevant element with the new data entered in the popup.

My question is, when returning to the main activity I currently use public void onResume() followed by code to populate the elements with the data, but is this the correct way to do it as when I return to the main activity, all the other data from other elements is gone. This is not good.

Each data field (EditText) of each element (Mon1, Mon2, Mon3, Tues1, Tues2 etc) has a unique id if that helps (or hinders).

Thanks

2 Answers2

0

Are you talking about a Dialog? You do not need to create a new Activity for that. Create a Custom Dialog and pass the argument back with a onDialogPositiveClick() Listener.

First, your MainActivity should implement TablesDialogFragment.NoticeDialogListener.

Then, in your dialog class:

public class TablesDialogFragment extends DialogFragment implements OnLongClickListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final ArrayList<String> mSelectedItems = new ArrayList<String>(); 
    String title = getArguments().getString("title");
    final String[] tables = getArguments().getStringArray("key");
    ContextThemeWrapper ctw = new ContextThemeWrapper( getActivity(), R.style.AlertDialogCustom);



    AlertDialog.Builder builder = new AlertDialog.Builder(ctw);

    if(tables.length !=0){
        builder.setTitle(title)
              .setMultiChoiceItems(tables, null,
                  new DialogInterface.OnMultiChoiceClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which,
                   boolean isChecked) {
               if (isChecked) {
                   // If the user checked the item, add it to the selected items
                   mSelectedItems.add(tables[which]);
               } else if (!isChecked) {
                   // Else, if the item is already in the array, remove it 
                  mSelectedItems.remove(tables[which]);
               }
           }
       })
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK, so save the mSelectedItems results somewhere
                   // or return them to the component that opened the dialog
                   String[] selected = mSelectedItems.toArray(new String[mSelectedItems.size()]);

//Here I pass the String[] to the MainActivity
                   mListener.onDialogPositiveClick(TablesDialogFragment.this, selected);



               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {

                       //Collapse SearchView
                       MainActivity.getSearchMenuItem().collapseActionView();

               }
           });
    } else{
        builder.setTitle("Sorry to tell you this, but... :(");
        builder.setMessage("Why don't you try something different?")

        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //Collapse SearchView
               MainActivity.getSearchMenuItem().collapseActionView();
            }
        });

    }
        return builder.create();
}

Then, in your MainActivity.

private String[] selectedItems;

public void callDialog(){

    TablesDialogFragment newFragment = new TablesDialogFragment();
    Bundle args = new Bundle();
    String[] array = {"hey", "you"};
    args.putString("title", "Title of The Dialog");
    args.putStringArray("key", array);
    newFragment.setArguments(args);
    newFragment.show(getSupportFragmentManager(), "Table");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog, String[] selected) {
    // TODO Auto-generated method stub
    selectedItems = selected;

            //Do Whatever you want with the arguments in your MainActivity

}
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
0
  1. If you override onResume() be sure to chain to super.onResume() before performing your custom logic.

  2. When you start a second activity and want to get a result back, use startActivityForResult(). Then to get the result, override onActivityResult(). See Starting Activities and Getting Results for more details. Also How to manage `startActivityForResult` on Android? and How to return a result (startActivityForResult) from a TabHost Activity? are related questions here on SO.

  3. You can use a Dialog instead of a second Activity as others have already suggested.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268