Search code examples
androidfragmenttransactionandroid-nested-fragmentfragmentmanager

Nested fragment-No activity to handle intent


I have an activity with fragment A added to it dynamically. Now, I transact from fragment A to fragment B. And then from fragment B to fragment C. Now I have a button here when clicked will place a call. I'm not able to start the call intent and the error log says illegal state exception - No activity found to handle the intent. Does it mean that there's no activity found for the nested fragment C? How can this happen (fragment creation without activity)? Also all fragments are dynamically created during run time. I tried this using both getfragmentmanager() and getchildfragmentmanager() method. I face this error in both the cases. Any suggestions would be much appreciated.

Here's my code snippet

Attaching Fragment A to activity

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container,FragmentA_.builder().build());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

Replacing Fragment A with Fragment B

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, FragmentB_.builder().build());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

Replacing Fragment B with Fragment C

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, FragmentC_.builder().build());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

Call intent

public void call(String contact){
Intent i=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+contact));
getActivity().startActivity(i);
}

In my manifest.xml I have added all permissions for Call.

Edit - 2 Fragment C code

@EFragment(R.layout.fragment_c)
public class FragmentC extends Fragment
{
@Click(R.id.call_button)
void call(){
call(phone_number);
}
  public void call(String contact){
    Intent i=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+contact));
    getActivity().startActivity(i);
    }
}

Solution

  • Remove getActivity() and Call directly startActivity(i)

    Fragments have its own startActivity() method. You don't need to pass activity context or reference in this case. For more information: Click here