11

Found numerous post regarding my issue but none of them worked out for me. I have a fragment from where I have to call startActivityForResult() on a textview click. From there, an activity will open up where user will fill some data and submit it to database. From there, I have to pass an intent containing the result code. But the onActivityResult() in fragment is not get called.

Code from my fragment

Intent in = new Intent(getActivity(), NetBarrelActivity.class);
        in.putExtra(AppUtility.ORDER_ID, orderDAO.getOrderNum());
        in.putExtra(AppUtility.TICKET_ID, 2);
        startActivityForResult(in, AppUtility.REQUEST_CODE);

Code from my activity

Double NetBarrels = crudeNetCalculator(GrossBarrels,
                        ProductObsTemp, ProductObsGravity, ProductBSW);
                db.updateTank(OrderID, TicketTypeID, CarrierTicketNum,
                        TankNum, TankTypeID, ProductObsTemp,
                        ProductObsGravity, ProductBSW, GrossBarrels,
                        NetBarrels);
                Intent in = new Intent();
                setResult(RESULT_OK, in);
                finish();

onActivityResult in Fragment:

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityReslt called");
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case AppUtility.REQUEST_CODE:
        if(resultCode == Activity.RESULT_OK){
            if (mOrdersDAO.getTicketType().equals(AppUtility.NET_BARREL)) {
                ArrayList<OrderTicketsDao> mOrderTicketsDaos = dbHandler
                        .getOrderTicket(mOrdersDAO.getOrderNum());
                if (mOrderTicketsDaos != null && mOrderTicketsDaos.size() > 0) {
                    TankAdapter mTankAdapter = new TankAdapter(getActivity(),
                            mOrderTicketsDaos);
                    listTank.setAdapter(mTankAdapter);
                    mTankAdapter.notifyDataSetChanged();
                }
            }
        }
        break;

    default:
        break;
    }
}

So, how do I send intent back to my fragment with result code?

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • You should be able to find a solution for your problem [here][1] [1]: http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment – Exoit Sep 08 '13 at 00:18
  • Thanks, I already visited the link. Did n't worked – Nitish Sep 08 '13 at 00:26
  • Although I am bit late, but this is how it has worked for me, implement the `onActivityResult()` in the `Activity` itself, receive the `Intent` extras and pass them to a method of `Fragment` which does relevant stuff on updating or retrieving data. – Rachit Mishra Sep 08 '13 at 09:08

1 Answers1

20

Since, I wanted to my fragment to intercept the intent so I used LocalBroadcastManager.

In onCreate of Fragment, I defined:

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            mUpdateUIReceiver,
            new IntentFilter(String action));

My receiver in fragment:

private BroadcastReceiver mUpdateUIReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        //do Stuff
    }
};

Activity code which will send broadcast:

LocalBroadcastManager.getInstance(this).sendBroadcast(
            new Intent(String action));

Hope it helps someone who is facing same problem.

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • 1
    I find this to be a very clean way to pass results when using fragments and childfragments. thanks for the suggestion. – TrueCoke Oct 13 '13 at 12:05
  • Thanks for very useful answer that solved my problem! I needed also to send some text from another activity to fragment, which can be easily done with adding extra values to intent. – Micer Oct 27 '13 at 16:35