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?
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.
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.
You will receive this call immediately before onResume() when your activity is re-starting. Show activity on this post.
onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With