I have done many programs, where I have implemented multiple Fragments inside a Single Activity, but not when using Single Activity to host multiple Fragments as Tabs and then on Tap show another Fragments...
Using MaterialViewPager library, in which I am calling different different Fragments to show views in their respective Tabs.
Like For the First Tab, I am using two Fragments, where
In First Fragment, I am using RecyclerView... to show list of Menus.
And in Second Fragment, I am using RecyclerView... to show list of Items under particular Menu.
So here my question is How to call Fragment from Fragment.
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Value value = valueList.get(position);
List<Learning> learning = value.getLearning();
// using when putting "item" data into same recyclerview
// but on back press just exiting, not showing list of Menus again
/**
learningAdapter = new LearningAdapter(learning, R.layout.card_learning, getActivity());
mRecyclerView.setAdapter(learningAdapter);
**/
ItemFragment fragment = new ItemFragment();
replaceFragment(fragment);
}
Method replaceFragment
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// using Fragment not Activity, so where can I use frame_container in xml
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
you can have a callback interface that is implemented by the activity which hosts these two fragments. Fragment A will use the call back to notify the activity to replace A with fragment B. Depending on your need, you can pass parameters across through the callback method itself.
Your callback interface:
public interface YourCallback {
public void methodWhichReplacesFragmentAwithB(params...);
}
Your Activity hosting fragments:
public class YourActivity extends ... implements YourCallback {
..
..
#Override
public void methodWhichReplacesFragmentAwithB(params...) {
//insert replace fragment code here
}
}
Your fragment will have a callback object, YourCallback callbackObj;. This callback object can be initialised using the activity (pass as this from activity) itself since the activity has the implementation of the interface. Now, you can use
callbackObj.methodWhichReplacesFragmentAwithB(actual_params...);
to replace the fragment. This callback interface can be exploited for other communications to parent Activity as well as other fragment hosted in that activity.
To replace fragment,
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment).addToBackStack("null").commit();
You can try this:
YourFragment fragment = new YourFragment();
FragmentTransaction transaction = ((AppCompatActivity) mContext).getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
There is One option which i have been using from long time before. I will added the solution with clear example.
So you want to pass the value from fragment A to fragment B
So, here what you want to do is, you have pass the value through the activity.
1) you pass the value from fragment A to activity
2) then activity to fragment B
Step 1: first create an Interface Like below in fragment A
public interface HomePage {
void onHomeButtonsClicked(String clickedButton);
}
public void setOnHomeButtonClicked(HomePage homePage) {
this.homePage = homePage;
}
Step 2: then create the instance like below in Activity. where fragment created in PagerAdapter
((FragmentA) fragment).setOnHomeButtonClicked(new FragmentA.HomePage() {
#Override
public void onHomeButtonsClicked(String buttonClicked) {
pageSelected.onHomeButtonsClicked(selectedPage);
}
}
});
Step 3: then create the interface in Activity like below.
public interface PageSelected {
void onHomeButtonsClicked(String clickedButton);
}
public void setOnHomeButtonClicked(PageSelected pageSelected) {
this.pageSelected = pageSelected;
}
step 3: then add the following method in fragment B in onCreateView or onResume()
((MainActivity) getActivity()).setOnHomeButtonClicked(new MainActivity.PageSelected() {
#Override
public void onHomeButtonsClicked(String clickedButton) {
//fragment B received the value here
}
});
Step 5: finally add the line in fragment A in button click or where you want to pass the value.
homePage.onHomeButtonsClicked("Some String , by this code it is in String. you can change in your own data type");
You can rise your question if you face any difficulties.
Related
Problem 1
I have a Navigation Drawer and most of my fragment transactions happens from here.
So say I have 4 Items in my drawer and I am doing the transaction from all of them. So if I am at the fragment [A] and now I click on the fragment [B], I need to come back to the previous fragment i.e. [A]. But if I keep clicking on the Item B of the navigation drawer that opens the fragment [B], I keep adding it to the backstack and when I press the back button, I am still at the same fragment.
Problem 2
How do I achieve the Clear Top behavior that is used for the intents for the fragments. As intents have the power to clear the activities from the stack from the top only, I want to achieve the same behavior.
Problem 1 & 2 solution Idea:
Create an Interface say FragmentInstanceHandler
public interface FragmentInstanceHandler {
public void openFragment(Fragment fragment, String fragmentTag);
}
Create a BaseFragment like below and extend this to all your Fragment classes:
public BaseFragment extends Fragment {
public FragmentInstanceHandler fragmentInstanceHandler;
public void setFragmentInstanceHandler(FragmentInstanceHandler fragmentInstanceHandler) {
this.fragmentInstanceHandler = fragmentInstanceHandler;
}
}
Implement the FragmentInstanceHandler interface to the Activity in which you are going to open all the Fragments. Let's say Activity is MainActivity:
public MainActivity extends Activity implements FragmentInstanceHandler {
private BaseFragment currentFragment;
#Override
public void openFragment(BaseFragment fragment, String fragmentTag) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment oldFragmentInstance = fragmentManager .findFragmentByTag(fragmentTag);
boolean fragmentPopped = fragmentManager.popBackStackImmediate (fragmentTag, 0);
if (!fragmentPopped && oldFragmentInstance == null) {
fragment.setFragmentInstanceHandler(this);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, fragment, fragmentTag);
fragmentTransaction.addToBackStack(fragmentTag);
fragmentTransaction.commit();
currentFragment = fragment;
} else if(fragmentPopped ){
currentFragment = oldFragmentInstance;
}
if(mDrawerLayout!= null)
mDrawerLayout.closeDrawers();
}
}
Now whenever you want to open a new Fragment even from any other Fragment you can call method like below, It is advised to provide new tag if you want to have new instance of same Fragment:
fragmentInstanceHandler.openFragment(new MyFragment(), "FragmentNewInstance");
You can tweak FragmentInstanceHandlerto add your own method to replace the current Fragment instead of adding. Above solution just gives you an idea how you can acheive your solution by putting and mananging all your code from one place.
I have a view pager in my Activity. This pager loads 2 fragments (Fragment1 and Fragment2).
My Activity have a button for fetching data from server as a list of my pojo class.
Fragment1 and Fragment2 contains recyclerView.
My question is How do I refresh Fragment1's recyclerView adapter (from my Activity) when information is fetched in my Activity?
I created an interface in my Activity:
public interface IloadCallBack {
void onLoadAdapter(List<Suser> userList);
}
and I have created a setter for this:
public void setIloadCallBack(IloadCallBack iloadCallBack) {
this.iloadCallBack = iloadCallBack;
}
and init it:
iloadCallBack.onLoadAdapter(susers);
Now, I have make a reference of activity into my fragment but I think this is wrong!! yes? what can i do?
How can do refresh recyclerView adapter in fragment 1 from my Activity when information fetched at my activity
You do not need the callback mechanism to pass data to fragment, hosted in activity.
Just create a method in fragment refreshList
// in fragment
public void refreshList(List<Suser> userList){
this.userList.clear();// empty list
this.userList.addAll(userList);
notifyDataSetChanged();
}
Keep a global reference to fragment instance and invoke refreshList from where you receive the response.
public class YourActivity...{
private Fragment fragmentInstance;
void someMethodReceivedNewList(){
// where you receive new list in activity
if(fragmentinstance!=null)
fragmentinstance.refreshList(userList);
}
void someMethodToLoadFragment(){
fragmentInstance = new YourFragment1();
...
}
}
Communicating from activity to fragment:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
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();
}
}
}
Communicating from fragment to activity:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
Both are taken from https://developer.android.com/training/basics/fragments/communicating.html
Feel free to have a look there, they explain everything very well.
if you want to perform some action when any specific event happens in some other place like if you want to execute any method in your fragment when an event happened in your activity or vice versa, I will suggest you should use EventBus.
https://github.com/greenrobot/EventBus
This is the simple and straightforward solution.
I'm trying to send data from an activity to a fragment. I'm not sending data from a fragment to an activity. I've got everything set up correctly other than instantiating the interface listener object in the activity.
public class Activity extends AppCompatActivity {
private FragmentInterface fragmentInterfaceListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This line below is actually in a button onClick()
fragmentInterfaceListener.sendDataMethod(dataToSend);
}
public interface FragmentInterface {
void sendDataMethod(SampleData sampleData);
}
}
Then in the fragment, I have:
public static class CustomFragment extends Fragment implements Activity.FragmentInterface {
#Override
public void sendDataMethod(final SampleData sampleData) {
}
}
When I put a log line in the button onClick(), the log line appears when the button is clicked. No, I'm not going to put the sampleData in a fragment bundle. Yes, I need to send the data through an interface. So how do I correctly instantiate the fragmentInterfaceListener object in the Activity? Am I missing anything else in the Activity or CustomFragment?
What your are missing here is the registering part.
The fragment has to register itself with the activity listener for the activity to send data when an event occurs.To do this create a method in activity
private void setOnDataListener(FragmentInterface interface){
fragmentInterfaceListener=interface;
}
And in the oncreate of your fragment set the listener like this
((YOUR_ACTIVITY_NAME)getActivity()).setOnDataListener(this);
You don't need to use listener in the Fragment because you can directly communicate with the Fragment from its host Activity.
As #lq-gioan says, you can create a public method in your Fragment then call it from your activity. So, create a public method to set the data, something like this:
public static class CustomFragment extends Fragment {
// public method to be accessed by host activity.
public void sendDataMethod(final SampleData sampleData) {
}
}
Then you can call the method within your host activity:
CustomFragment fragment = (CustomFragment)getSupportFragmentManager()
.findFragmentById(R.id.fragment_id);
// or use find by tag if you adding the fragment by tag
// CustomFragment fragment = (CustomFragment)getSupportFragmentManager()
// .findFragmentByTag("FragmentTag");
// now you can call it
fragment.sendDataMethod(yourSampleData);
For sending data from Activity to Fragment we don't need an interface.
You can directly call the method in Fragment or pass as setArguments in Bundle
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
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();
}
You can refer https://developer.android.com/training/basics/fragments/communicating.html
We are going to write an app, in one section of app user enter to an Activity that call it PostDetailsActivity.
In this Activity we have a fragment call it PostDetailsFragment. We apply an Interface to communicate between this fragment and activity
public interface changeFragment{
void replaceFragment(Fragment fragment, String backStack);
}
In PostDetailsFragment we have post and it's related posts. related post listed in recyclerview bu an adapter. when user click on Item in adapter, adaoter act like this:
PostDetailsFragment fragment = PostDetailsFragment.newInstance(GsonString);
mListener.replaceFragment(fragment, fragment.getFragmentKey());
and in Activity we act like this:
#Override
public void replaceFragment(Fragment fragment, String backstackName) {
if(fragment instanceof PostDetailsFragment){
fragmentPostDetails = (PostDetailsFragment) fragment;
}
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(backstackName);
fragmentTransaction.commitAllowingStateLoss();
}
What we doing: we create one Activity and if user click on related posts, activity just replace fragment with new instance of PostDetailsFragment with new data.
Issue: after five or more and less, App crashed and got OoutOfMemory Error!
I attache MAT too
How we should manage this problem?
I have an activity A with 3 fragments. Each fragments replaces each other, hence at a given time only 1 is visible.
HomeFragment has 2 textviews wrapped inside 2 cardviews. Each cardview represents a text value which comes from Fragment1 and Fragment2. When I click on say Card1,I get to the Fragment1.
Fragment1 has some cardviews, when I selects any of them I navigate back to HomeFragment and update the cardview text based on my selection in Fragment1.Here is the switch statement, depending upon what card user selects I put that in a bundle and pass it to HomeFragment.
switch (v.getId()) {
case R.id.card_view0:
Fragment1Bundle.putString("Test", "Testing");
bundle.putBundle("Fragment1Bundle", Fragment1Bundle);
fragmentTransaction.setCustomAnimations(R.anim.slideup, R.anim.slidedown, R.anim.slideup, R.anim.slidedown);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
Fragment2 has same behavior as Fragment 1.
switch (v.getId()) {
case R.id.card_view0:
Fragment2Bundle.putString("Test2", "Tetsing");
bundle.putBundle("Fragment2Bundle", Fragment2Bundle);
fragmentTransaction.setCustomAnimations(R.anim.slideup, R.anim.slidedown, R.anim.slideup, R.anim.slidedown);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
My challenge is that I am using bundles to pass data between fragments, My home fragment gets updated with the data it from fragment1 but when I go to fragment 2 and after making the selection come back to Home fragment, my fragment1 data is set to default. This is what I am doing in Home Fragments onCreateView()
try {
bundle1 = getArguments().getBundle("Fragment1Bundle");
bundle2 = getArguments().getBundle("Fragment2Bundle");
tv.setText(bundle1.getString("Test") == null ? null : bundle1.getString("Test"));
tv2.setText(bundle2.getString("Test2") == null ? nul : bundle2.getString("Test2"));
} catch (NullPointerException e) {
Log.d(TAG, e.printStackTrace());
}
I know that I am creating a new Homefragment in my fragment transaction in both fragment1 and fragment2, How can I keep just 1 instance of Home fragment around.
Another design recommended by Google is to use the main Activity and 2 fragments (in your case Fragment1 and Fragment2). I can see your problem of passing data bundle to HomeFragment. This suggested design uses MainActivity which is declared static (may be required for scoping issue). And it uses an interface to be established between Activity and a Fragment. I think the interface is easier than passing bundle back to the HomeFragment.
A Google webpage is # Communicating with Other Fragments. This is not just my opinion. A good SO link, I think, is How to pass data between fragments.
Code snippet from the webpage...
An example of Fragment to Activity communication:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
...
An example of Activity to Fragment communication:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
Note:
OnHeadlineSelectedListener is the interface created by the Fragment.
The created method onArticleSelected has a parameter position, which comes from the ListView in ListFragment (in the sample).
You can still set data bundles and send data between Activity and Fragment. However I have not sent back data from Fragment to Activity. I normally use Fragment to handle much of UI updates.
how to pass values from activity to already open fragment and update array-list help me please. when I using interface the array-list size is zero what I do? do not us bundle method.
public class Main2Activity extends AppCompatActivity{
String desc = "data";
OnDataPassedListener onDataPassedListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
String passedArg = getIntent().getExtras().getString("id");
Log.d("data",passedArg);
Scription scription = new Scription();
onDataPassedListener = (OnDataPassedListener)scription;
onDataPassedListener.onDataPassed(passedArg,desc);
}
public interface OnDataPassedListener {
void onDataPassed(String text,String name);
}
}
public class Test extends Fragment implements
Main2Activity.OnDataPassedListener{
.
.
.
.
#Override
public void onDataPassed(String text,String name) {
monthlylists.get(Integer.valueOf(text)).setFood_type(name);
}