BroadcastReceiver mBroadcastReceiver;
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Now just call your Fragment here now.
CustomerOutStandingFragment outStandingFragment = new CustomerOutStandingFragment();
// AppartmentServices1 fragment2 = new AppartmentServices1();
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, outStandingFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Bundle bundle = new Bundle();
bundle.putInt("CUSTOMER_ID", CustomerListAdapter.customerId);
bundle.putString("CUSTOMER_NAME", CustomerListAdapter.customerName);
Log.d("SID:", "" + CustomerListAdapter.customerId);
outStandingFragment.setArguments(bundle);
}
};
@Override
public void onResume() {
super.onResume();
context.registerReceiver(mBroadcastReceiver, new IntentFilter("com.example.vendorapp.fragment.CustomerListFragment")); // This code is in your Activity will be in onCreate() or in onResume() method.
}
@Override
public void onPause() {
super.onPause();
context.unregisterReceiver(mBroadcastReceiver);
}
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Now just call your Fragment here now.
CustomerOutStandingFragment outStandingFragment = new CustomerOutStandingFragment();
// AppartmentServices1 fragment2 = new AppartmentServices1();
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, outStandingFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Bundle bundle = new Bundle();
bundle.putInt("CUSTOMER_ID", CustomerListAdapter.customerId);
bundle.putString("CUSTOMER_NAME", CustomerListAdapter.customerName);
Log.d("SID:", "" + CustomerListAdapter.customerId);
outStandingFragment.setArguments(bundle);
}
};
@Override
public void onResume() {
super.onResume();
context.registerReceiver(mBroadcastReceiver, new IntentFilter("com.example.vendorapp.fragment.CustomerListFragment")); // This code is in your Activity will be in onCreate() or in onResume() method.
}
@Override
public void onPause() {
super.onPause();
context.unregisterReceiver(mBroadcastReceiver);
}
In My adpater:-
viewHolder.viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
customerId = customerArrList.get(i).getCustomerId();
customerName = customerArrList.get(i).getCustomerName();
Log.d("Customer Id -->", "" + customerId);
mContext.sendBroadcast(new Intent("com.example.vendorapp.fragment.CustomerListFragment"));
Log.d("BroadCast Active -->", "yes");
}
});
For an Activity:
In order to register your broadcast receiver from within your app, first, remove the <receiver> tag from your AndroidManifest.xml file. Then, call registerReceiver(BroadcastReceiver receiver, IntentFilter filter) in your onResume(). Use unregisterReceiver(BroadcastReceiver receiver) in your onPause() to unregister the Broadcast receiver.
EDIT: Sample Code:
public class MyActivity extends Activity
{
private final BroadcastReceiver mybroadcast = new SmsBR();
public void onResume()
{
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mybroadcast, filter);
}
public void onPause()
{
unregisterReceiver(mybroadcast);
}
}