Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: onActivityResult in fragment not called

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?

like image 566
Nitish Avatar asked Sep 08 '13 00:09

Nitish


People also ask

Can we use onActivityResult in fragment Android?

Since Activity gets the result of onActivityResult() , you will need to override the activity's onActivityResult() and call super. onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.

How can I call onActivityResult of fragment from activity in Android?

Within your fragment, you need to call: startActivityForResult(myIntent, MY_INTENT_REQUEST_CODE); where myIntent is the intent you already defined, and MY_INTENT_REQUEST_CODE is the int constant you defined in this fragment as a global variable as the request code for this intent.

Is onActivityResult called before onResume?

You will receive this call immediately before onResume() when your activity is re-starting. Show activity on this post.

Is startActivityForResult deprecated?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


1 Answers

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.

like image 89
Nitish Avatar answered Nov 15 '22 19:11

Nitish