I am trying to call a Fragment class from Activity using Intent. Is it possible to implement. Please provide your views.
Fragment needs to be hosted by a FragmentActivity, you can't add a fragment via an Intent.
You need to create a FragmentManager to add your fragment in the FragmentActivity (or call another FragmentActivity via an Intent and add your fragment on it).
See this topic for more information: Add a Fragment to an Activity at Runtime.
Fragment TargetFragment=new target_fragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content,TargetFragment);
transaction.addToBackStack(null);
transaction.commit();
Intent can not be applicable to Activity to Fragment So there Is Another method
getSupportFragmentManager().beginTransaction().replace(R.id.container,new DashBoardFragment()).commit();
Related
I have an activity(Activity1) with several fragments. I'm calling a second activity(Activity2) from one of the fragments(say Fragment C) of first Activity. I want to navigate back from the second activity(Activity2) to Fragment C. But, it is navigating to the first fragment of Activity1 instead of Fragment C. Please help.
Block of code I have tried so far:
In Fragment C,
categoryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(getActivity(),Activity2.class);
getActivity().startActivity(i);
}
});
In the container Activity i.e Activity1,
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
FragmentC myFragment = new FragmentC();
transaction.replace(R.id.frame_container, myFragment);
transaction.commit();
According to the Official Android Documentation the best way to have Fragment to communicate with another even to another Activity is through the associated Activity.
To avoid any mess with Fragment Transaction please referring to this:
Often you will want one Fragment to communicate with another, for
example to change the content based on a user event. All
Fragment-to-Fragment communication is done through the associated
Activity. Two Fragments should never communicate directly.
I have container activity that has framelayout for storing fragment inside it.
For example I have activity with initial framgent for login page. Theare are two buttons Login And Register.
User can click register and in this case I need to change my current fragment with another fragment with registration fields.
So I have only idea to make an host activity implement callback interface and than pass activity to fragment, and when user clicks register callback method is called and host activity replacing fragment.
So the question is if there is any other better way to do this, maybe more correclty.
Please leave your suggestions and how do you deal with this problem.Thanks
You can replace fragments as follows ,
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(<container id>, new RegisterFragment());
transaction.commit();
//if you are using support library, make sure to use getSupportFragmentManager()
is there a way to send some data from an activity to a running fragment?
In my app I'm adding a second fragment over another fragment. I intentionally use the add method instead of the replace method. So now I want to hide my second fragment with
fragmentManager.popBackStack();
and my first fragment reappears. After hiding the second fragment I want to send some data from my activity to the still running frist fragment.
Any idea how to do this? It doesn't work with bundles (put extra), because I don't rebuild the fragment, I just hide the second one!
one simple solution is:
MyFragment oldFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fragment_place);
fragmentManager.popBackStackImmediate();
MyFragment newFragment = (MyFragment)fragmentManager.findFragmentById(R.id.fragment_place);
newFragment.postData(...);
You can use an EventBus library like this one, it's easy to use and very convenient.
You can use tags on the Fragments when you create them to call them when needed.
getFragmentManager().beginTransaction()
.add(R.id.content, new SomeFragment(), SomeFragment.class.getSimpleName())
.commit();
So above I use the simple name of the class to tag the fragment when I create and add it to the activity.
SomeFragment fragment = (SomeFragment) getFragmentManager().findFragmentByTag(SomeFragment.class.getSimpleName());
And I can call it back when I need it and know it is being displayed like above, now I can call send it data like normal by calling a public method in the custom fragment and passing it the data as a param.
My parent activity is launching another activity and needs its fragments to update when the results are returned via onActivityResult. Should I simply implement methods in each fragment to pass the data into them, or is there a different way this should be done?
Thanks!
Make your fragment that should receive data implement an interface with a suiting method.
In the activity when the result is returned from onActivityResult, use the getFragmentManager().findFragmentByTag("yourtag") method to find the fragment. Cast it to the interface and call the method is has.
Make sure to check if the fragment actually exists after calling findFragmentByTag.
If the fragment is the one making the startActivityForResult call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once you implement super.onActivityResult for all unhandled results, the fragment got a shot at handling the result.
To get the result in your fragment make sure you call : startActivityForResult(intent,123); instead of getActivity().startActivityForResult(intent,123); inside your fragment.
I hope this will solve your problem.
Someday I will have to solve your issue or similar. Per my understanding, you'll have to implement an interface between Activity and a Fragment. The link is at Communicating with Other Fragments . The webpage seems clear unlike other Google pages. A snippet of code:
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Almost always, class FragmentTransaction is involved in managing data. Note how Bundle is used to pass data, also to get data from the client side.
Have fun, keep us posted.
I have an Activity called Mytaskonclick extends Activity and a Fragment called SentTaskFragment extends Fragment. I want to go from Mytaskonclick to SentTaskFragment on click of a button. I tried using the code
Intent ii=new Intent(Mytaskonclick.this,SentTaskFragment.class);
startActivity(ii);
But this code doesn't work. Can anyone suggest me how to do it?
Fragments does not work like this.You need to put a place holder in your activity, say FrameLayout for example and, inside on click, you get the fragment and attach it inside this placeholder. I suggest you read the Fragment Tutorial Here. It contains all what you need to know and extremely useful for you.
When you want to switch to a Fragment them you have to do that through FragmentManager. Pass that thae Fragment object into beginTransaction() method of FragmentManager along with the container layout which will hold the Fragment as below...
SentTaskFragment fragment = new SentTaskFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment).commit();
You can see some tutorial from below link...
Android Developer-Fragment
Multi-pane development in Android with Fragments - Tutorial
Android Fragments
Android Fragments Example