Start a fragment from an activity - android

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.

Related

invoke fragment from a fragment

by pressing a button inside the main fragment, I wanna call the second fragment in that place
public void onViewClicked(View view) {
Fragment frag = null;
switch (view.getId()) {
case R.id.btn_login:
frag = new LoginFragment();
break;
case R.id.btn_offline:
frag = new OfflineFragment();
break;
}
MainActivity mainActivity = new MainActivity();
FragmentManager manager =
mainActivity.getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment, frag);
transaction.commit();
}
Never create an Activity instance . Its a Component and managed by system itself(Intent). Use getActivity() to get the Context of Activity.
FragmentManager manager =
getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment, frag);
transaction.commit();
You should take a look at Android Application Fundamentals.
I would suggest to go with getChildFragmentManager(); of the fragment to get the fragment manager to perform all your fragment related operations instead of getting it from using the fragmentManager from the getActivity() method. It help you a lot on reloading the fragment on resume of it again at later point of time.

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);
}
}
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

start Fragment from RecycleView Adapter Onclick

Hi I have A RecycleView Adapter and A button. I want that button to start a Fragment. I can start an activity but not a fragment. I have tried this Onclick method for my Button
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putParcelable("event", events.get(getLayoutPosition()));
Fragment fragment = new EditEventDetailFragment();
fragment.setArguments(bundle);
fragment.getFragmentManager().beginTransaction().replace(R.id.contentMainDrawer,fragment).commit();
}
But have error invoke null object (contentMainDrawer) is my Main activity content_layout.
Any help is much appreciate. The Fragment host recycle view is call from Mainactivity
Use below code for replace fragment
Fragment fragment = new EditEventDetailFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();
if you are using this code inside adapter than replace getActivity() to ((Activity) context).
Hi I have found 2 answer that help whoever needed. The answer is to use the Activity that host the calling fragment. Many thanks #Mohit Suthar
Bundle bundle = new Bundle();
bundle.putParcelable("event", events.get(getLayoutPosition()));
Fragment fragment = new EditEventDetailFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((MainActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();

Redirect the fragment when click on button

I am using this code for replacing the fragment but there is a problem, it's not replacing the old fragment it just override on old fragment so please tell me what is the problem here.
public void selectFrag1(View rootView) {
Fragment frag;
if (rootView == findViewById(R.id.startup1)) {
frag = new S_SignupFragment();
} else {
frag = new F_SignupFragment();
}
FragmentManager fragManager = getSupportFragmentManager();
FragmentTransaction fragTransaction = fragManager.beginTransaction();
fragTransaction.replace(R.id.fragment_signup,frag);
fragTransaction.commit();
}
Are you sure that you pass the parent view, that is going to hold your fragment on the line fragTransaction.replace(R.id.fragment_signup,frag); ?
For fragmentTransaction.replace(...,...) you need to specify a container for your fragment and the fragment itself. Check where does your R.id.fragment_signup directs. More information you could find in this SO question: Unable to replace fragment on button click
fr = new FragmentOne();
fm = getSupportFragmentManager();
fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place,fr);
fragmentTransaction.commit();
if you use same code then add this before call first button setonclicklistner

How to show second fragment from first fragment with in a single 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.

Categories

Resources