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?

3
  • You should be able to find a solution for your problem [here][1] [1]: stackoverflow.com/questions/6147884/… Commented Sep 8, 2013 at 0:18
  • Thanks, I already visited the link. Did n't worked Commented Sep 8, 2013 at 0: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. Commented Sep 8, 2013 at 9:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I find this to be a very clean way to pass results when using fragments and childfragments. thanks for the suggestion.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.