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);
}
}
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
Related
I have three fragments (fragmentA, fragmentB, FragmentC). The code goes from fragmentA to fragmentB and then fragmentB to Fragemnt C.
When I press the back button I go from fragmentC to fragmentA.
After the back button is pressed fragmentA is displayed but you can also see fragmentC behind it.
fragmentA
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentB)
.addToBackStack("Null")
.commit();
fragmentB
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentC)
.commit();
fragmentC
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentC)
.commit();
When the back button is pressed I want to go from fragmentC to fragmentA and not have fragment C displayed in the background
In addToBackStack use null as param , not "Null"
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentB)
.addToBackStack(null)
.commit();
you can try this when replacing fragment
Fragment frag=null;
frag=new Navigation_Help();
if(frag!=null){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction ft=fragmentManager.beginTransaction();
ft.replace(R.id.sacreenarea,frag);
//for not to back previous fragment remove next nile
ft.addToBackStack(null);
ft.commit();
}
I tried using #ADM duplicate link, it would take me to the fragment A but if I loaded fragment B for a second time I would get double commit errors.
The solution that works for me is to restart the activity with the #Override onBackPressed(), this will reload the fragmentA by default.
Fragment B
FragmentManger fm = fragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fm.replace(R.id.container, fragmentC, "fragmentC");
.commit();
fragmentC backbutton pressed
Activity
#Override
public void onBackPressed() {
if(getSupportFragmnetManager().findFragmnetByTag("fragmentC") !=null) {
Intent myIntent = new Intent(this, Activity.class);
startActivity(myIntent);
finish();
} else {
super.onBackPressed();
}
}
This solution will only work if you want to go back to the first fragment loaded by the Activity, so it may not be the best solution out there but it worked for me.
I have 2 fragment on one fragment activity. I have 2 fragments say new and pending but I am having problem while switching from one fragment to another while tapping of button. When I am on pending tab and then click on new tab then sometimes it shows two loader and then it still shows pending page. My both fragments are android.support.v4.app.Fragment. I am attaching screenshot of the problem while I am tapping on new tab and still showing pending tab with two loader.
code for switching between fragment
#Override
public void onClick(View v) {
if (v.equals(neww))
{
Fragment newpage = new NewPageActivity();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, newpage);
ft.commit();
}
else if(v.equals(pending))
{
Fragment pendingFragment = new PendingPage();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, pendingFragment);
ft.commit();
}
}
now screenshot of problem
Try this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelay, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
I m trying to start a fragment from an activity like this
public void btn (View view)
{
Intent intent1 = new Intent(this, F5_fr.class);
startActivity(intent1);
}
i have an Onclick thing in my xml with the "btn" :
Fragment 's name is F5_fr.class
Thanks in advance
Use this Snippet. Please refer what is fragment here
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction().replace(R.id.content, YOUR_FRAGMEMENT,
FRAGMENT_TAG(OPTIONAL);
transaction.addToBackStack(null);
transaction.commit();
Please check below code snippet :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
YourFragment fragment = new YourFragment();
fragmentTransaction.replace(android.R.id.content, fragment);
Referance link - this.
here you will get idea of fragments . you can see here how to call a fragments from an activity.
I am developing a simple android app only for tablets and using android 4.0. My application have the main screen like as follow:
Oncreate() of Main Activity I am adding Fragment A in the main.xml using following code:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment imageFragment = new ImageFragment();
ft.replace(R.id.fragment_container, imageFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
This fragment A just have only a Image view which is clickable. Now I want that when user click on Image view then another fragment (Fragment B) should call and it replace the image view. The Fragment B have a VideoView which play the video.
So My second screen should be like as follow:
My problem is I am not gettting "How to call second fragment from the first one with in main screen activity?"
I can use different activities but I do not want to do so and just want to run this using fragments.
Please guide me.
This is the simplest way:
1) Inside YourActivitycreate a method:
public void goToSecondFragment(){}
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment secondFragment = new SecondFragment();
ft.replace(R.id.fragment_container, secondFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
2) In your first fragment, when you want to replace it, call:
YourActivity activity = (YourActivity) getActivity();
activity.goToSecondFragment();
You can do the same using below:
Adding a fragment with maintaining a back stack
FragmentManager supportFragmentManager = getSupportFragmentManager();
supportFragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
}
});
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack("fragmentTag");
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
And Replacing a fragment
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
Should work for you.
I want to do the following. There are two fragments first and second. Necessary make the transition between them. When I go from first fragment in the second, first stored in the stack. When I click the Back button the second fragment is removed and returned first fragment from the stack. Again I can not go in the second fragment - it has been deleted. How can I solve this problem?
In main activity (callback for Fragment1):
#Override
public void onNavigate() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment1 newFragment1 = (Fragment1) getFragmentManager().findFragmentByTag("frag_1");
Fragment2 newFragment2 = (Fragment2) getFragmentManager().findFragmentByTag("frag_2");
ft.replace(R.id.main, newFragment2);
ft.remove(newFragment1);
ft.addToBackStack(null);
ft.commit();
}
Fragments I added dynamically:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.main, new Fragment1(), "frag_1");
ft.add(R.id.main, new Fragment2(), "frag_2");
ft.commit();
I solved this problem :). I hide first fragment and add transaction to the back stack. When I click button Back I return to fragment
#Override
public void onNavigate() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment1 newFragment1 = (Fragment1) getFragmentManager().findFragmentByTag("frag_1");
ft.hide(newFragment1);
ft.addToBackStack(null);
ft.commit();
}