Search code examples
androidbaseadapteronactivityresult

onActivityResult in my BaseAdapter is not firing


In my getView i have the following

holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                switch (v.getId()) {
                    case R.id.ivDelete:

                        PopupMenu popup = new PopupMenu(mContext, v);
                        popup.getMenuInflater().inflate(R.menu.transaction_list_menu,
                                popup.getMenu());
                        popup.show();
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {

                                switch (item.getItemId()) {
                                    case R.id.edit:

                                        Intent i = new Intent(v.getContext(), EditAmountDialogActivity.class);
                                        i.putExtra("table", table);
                                        i.putExtra("id", listItemId);
                                        ((Activity) mContext).startActivityForResult(i, Activity.RESULT_OK);

                                        break;
                                    case R.id.delete:

                                        AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                                        alertDialog.setMessage("Are you sure you want to delete?")
                                                .setCancelable(false)
                                                .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                                                    public void onClick(DialogInterface dialog, int id){
                                                        db.deleteTransaction(table, listItemId);
                                                        notifyDataSetChanged();
                                                    }
                                                })
                                                .setNegativeButton("No", new DialogInterface.OnClickListener(){
                                                    public void onClick(DialogInterface dialog, int id){
                                                        dialog.cancel();
                                                    }
                                                });
                                        AlertDialog alert = alertDialog.create();
                                        alert.show();

                                        break;

                                    case R.id.refresh:
                                        cursor = db.getTransactions(table);
                                        ((TransactionActivity)mContext).setBalances();
                                        notifyDataSetChanged();
                                        break;

                                    default:
                                        break;
                                }

                                return true;
                            }
                        });

in the R.id.edit i am starting activity for result

in the EditAmountActivityDialog.class i have the following

dialogButtonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                memo = etMemo.getText().toString().replace("'", "\'\'");
                timestamp = etDate.getText().toString();


                try {
                    amountDouble = Double.parseDouble(etAmount.getText().toString().replace(",", ""));
                } catch (NumberFormatException e) {

                }

                if (amountDouble == 0.0) {
                    Toast.makeText(getApplicationContext(), "You must enter an amount!",
                            Toast.LENGTH_SHORT).show();
                } else {

                    hideSoftKeyboard();
                    db.updateTransaction(id, table, type, amountDouble, memo, dbTimestamp);
                    db.close();
                    Intent i = new Intent();
                    setResult(RESULT_OK, i);
                    finish();
                }

Any idea why it is not activating the onActivityResult?

Thanks!


Solution

  • You are using the startActivityForResult and the setResult methods wrong.

    You should specify a request code for your startActivityForResult so it can be later used in onActivityResult to determine which request has a result available.

    Define a static and final variable on the top of your class:

    private static final int REQ_EDIT = 100;
    

    It can be anything. We are just saving it for later reference.

    Then start your activity for result:

     case R.id.edit:
         Intent i = new Intent(v.getContext(), EditAmountDialogActivity.class);
         i.putExtra("table", table);
         i.putExtra("id", listItemId);
         startActivityForResult(i, REQ_EDIT);
         break;
    

    So you now have started an activity for the result.

    To finish the activity with a result use the following code:

    ...
    hideSoftKeyboard();
    db.updateTransaction(id, table, type, amountDouble, memo, dbTimestamp);
    db.close();
    setResult(RESULT_OK);
    finish();
    ...
    

    This will return a OK result to your fragment.

    In your fragment, override the onActivityResult method like below:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQ_EDIT:
                if(resultCode == Activity.RESULT_OK) {
                    // Do something 
                }
                break;
        }
    }