I want to start a new activity inside the fragment boundaries itself instead of loading it in a complete new full screen.
I tried this :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Intent intent = new Intent(this,MenuFragmentActivity.class);
MenuFragment newFragment = new MenuFragment();
fragmentTransaction.add(R.id.menuFragment,newFragment);
newFragment.startActivity(intent);
fragmentTransaction.commit();
But this starts the activity in a new screen rather than in the confined fragment ?
The behavior you're seeing is correct.
For layout purposes, activities cannot be "children" of fragments. It's the other way around: fragments are children of activities. So, basically, what you're trying to do won't work.
You should read the full Fragments guide if you haven't already. Here's the relevant quote about layouts:
When you add a fragment as a part of your activity layout, it lives in
a ViewGroup inside the activity's view hierarchy and the fragment
defines its own view layout. You can insert a fragment into your
activity layout by declaring the fragment in the activity's layout
file, as a <fragment> element, or from your application code by adding
it to an existing ViewGroup.
Instead of starting a new activity, try just loading another fragment within the original fragment's layout.
Related
I followed this tutorial to build an app with three fragments in sliding view (ViewPager).
https://c1ctech.com/android-viewpager-example-to-create-sliding-screens/
The app was built successfully. Then I added a button inside FragmentOne. And also created another fragment named FragmentFour with fragment_four.xml layout file.
Now I want to link the FragmentFour with FragmentOne through onClickListener with the button I created before. When in FragmentOne the button will be clicked, and it'll launch FragmentFour. To achive that, inside onClickListener I added these codes-
FragmentFour fragFour = new FragmentFour();
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frameLayout, fragFour);
transaction.addToBackStack(null);
transaction.commit();
But after running the app, when I click on that button from inside the FragmentOne, FragmentFour's layout overlaps over the FragmentOne's layout. And FragmentTwo and FragmentThree can be accessible via sliding from FragmentFour, but I don't want this.
If anything wrong here, please help me.
Your code is replacing FragmentOne with FragmentFour in the ViewPager:
transaction.replace(R.id.fragment_one, fragFour);
which results in a ViewPager with FragmentFour, FragmentTwo, and FragmentThree
It seems like you want to start a new Activity for FragmentFour. From your onClick handler in FragmentOne, start a new activity as follows:
getActivity().startActivity(new Intent(context, FragmentFourActivity.class));
FragmentFourActivity is an Android Activity (e.g. AppCompatActivity) whose layout contains FragmentFour. Follow along at https://developer.android.com/training/basics/firstapp/starting-activity if unsure how to do so.
The problem is now gone. I just need to make few changes to my codes. Inside onClickListener, I changed my codes like this-
Before:
FragmentManager fragmentManager = getChildFragmentManager();
After:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
And inside the fragment_four.xml file's relative layout:
android:background="#color/bgcolor"
android:clickable="true"
android:focusable="true"
I'm learning about fragments I have some doubts. Consider following code:
FragmentManager fm = getFragmentManager();
Fragment MyFragment = new Fragment();
fm.beginTransaction().replace(R.id.my_container, MyFragment).addToBackStack(null).commit();
My question is:
what exactly does replace do?
What happens if I create many fragments this way (to replace previous ones in a container).
Can it in any way be bad for memory usage?
Is it considerably better just to change fragment's content?
Replace removes all the fragments that are in the container and adds the new fragment to the container. (if there isn't a fragment in the container then it just adds the new one).
If you create many fragments this way then every transaction is saved to the backstack so you can reverse the transaction by pressing the back button.
The only thing you can do is to create a variable fragmentTransaction and use the fm.beginTransaction() only once and not every time you want to replace the fragment in the container.
I don't think so, fragments should be modular and reusable.
You can read more here:
https://developer.android.com/guide/components/fragments.html
it simple put another "layer" on container.
appcrash
yes
No, fragment is the easiest way.
Using fragment & backstack tag to reference to a Fragment if you want to call fragment again and process Back button.
fm.beginTransaction().replace(R.id.my_container, MyFragment, "FRAGMENT_TAG").addToBackStack("FRAGMENT_BACKSTACK_TAG").commit();
I have a FragmentActivity in which I am displaying two Fragments, Fragment A and Fragment B. In Fragment A I have another Fragment with Tabs. I have done this using classic TabHost. In this TabHost I have 2 more Fragments which has a ListView. Now what I want is on clicking on listitem I want to replace the content in Fragment B of parent FragmentActivity. Please help me to achieve this. I have tried the following till now.
1. View mContainer = (View)getActivity().findViewById(R.id.rightpane);
2. ViewGroup mContainer = (ViewGroup)getView().getParent();
3. Activity rrlist = ((RRPatientList)getParentFragment()).getActivity().getParent();
View mContainer = (View) rrlist.findViewById(R.id.rightpane);
ResultDetailView rdl = new ResultDetailView();
rdl.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(mContainer.getId(), rdl);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
Nothing is working and I am getting either NullPointerException or No View Found for the ID ****** errors.
Please refer the screen shot for more info.
create method(like: updateFragment) in your activity to update fragment B. from fragment A's child fragment call that method(updateFragment) then that method(updateFragment) update fragment B.
here is answer for update fragment from activity - it 'll help you to update fragment B from your parent activity
here is answer for call activity method from fragment - it 'll help you to call parent activity method from fragment A's child fragment
I'll jump straight to the point.
I'm making an application with has three tabs (MainFragment, FragmentCategories, FragmentList). What I want to do next is this:
From the second tab, FragmentCategories, I open up a certain category. Using getFragmentManager and FragmentTransaction, I open up a FragmentCategoriesList. From there, when I choose a certain object, I want to open it in yet another "subfragment".
Following a number of tutorials, MyFragmentCategoriesList uses a ListView layout.
context = getActivity().getApplicationContext();
view = inflater.inflate(R.layout.fragment_categories_list, container, false);
Later in the code, I initialize a ListView object which uses that same layout (R.layout.fragment_categories_list == R.id.frag_cat_list).
final ListView lv1 = (ListView) view.findViewById(R.id.frag_cat_list);**
lv1.setAdapter(new MyCustomBaseAdapter(context, searchResults));
Here's a snippet where I select an object:
...
data.putString("selection",test);
FragmentManager fragManager = myContext.getSupportFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
FragmentCategoriesDetail FragCatDetail = new FragmentCategoriesDetail();
FragCatDetail.setArguments(data);
fragTrans.add(R.id.frag_cat, FragCatDetail);
fragTrans.addToBackStack(null);
fragTrans.commit();
...
Anyway, the problem is this:
fragTrans.add(R.id.frag_cat, FragCatDetail);
In this case, my FragCatDetail works, but I can't go BACK to the FragmentCategoriesList. If I put R.id.listview (a relative layout xml containing two textviews), my Fragment opens WITHIN an object of a ListView, and I can go back to FragmentCategoriesList.
If I put R.id.frag_cat_list (which is, after all, the layout of the current fragment, FragmentCategoriesList), my app crashes with
java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
which seems logical because FragCatDetail is a Fragment with a relative layout.
My question is this: what can I do in order to have my FragCatDetail fragment work like in the first case (relative layout) AND go back to FragmentCategoriesList like in the second case (listview layout).
Thanks in advance! Much appreciated.
Figured out what was the problem.
FragmentManager fragManager = myContext.getSupportFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
FragmentCategoriesDetail FragCatDetail = new FragmentCategoriesDetail();
FragCatDetail.setArguments(data);
fragTrans.add(R.id.frag_cat, FragCatDetail);
fragTrans.addToBackStack(null);
fragTrans.commit();
Throughout the application, I used
getFragmentManager().popBackStack(). Since I was going to a new fragment (FragmentCategoriesDetail), I was doing that by getSupportFragmentManager.
In my Main Activity, I overrode onBackPressed() and inside I was using
getFragmentManager().popBackStack();
where in fact I should have been using
getSupportFragmentManager().popBackStack();
at least for this specific fragment.
Once again, it is a story of a FragmentManager vs SupportFragmentManager. I hope I helped someone with this. :)
I'm wondering if it would be possible, to use the XML-onclick attribtue to call another XML-"View".
Means if I'm in a menu and click on the button "Create new Drawing", shouldn't I be able to call just another XML-View to bring up more options?
What I know is: android:onclick="hellothere" calls the method public void hellothere(View view) in the Activity to whom the view belongs, but why does it has to be used with code, instead that I'd just call another XML-Layout?
Example: andorid:onclick="new_page" (new_page.xml opens)
Calling another xml without a context is not possible in android. The context would be something like an activity, fragment etc.
If you want to inflate another xml into an area on your desired layout without activity transition, use fragments instead.
Heres's a simple code how to attach a fragment in a specific or generic area:
attaching to generic fragment area:
FragmentTransaction ft;
Fragment mFragment = new MySampleFragment();
ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, mFragment, "samplefragment");
ft.commit();
as you can see above, we attach the Fragment into the default fragment inflater of android which is "android.R.id.content".It gives you the root element of a view, without having to know its actual name/type/ID. Check out Get root view from current activity
attaching to specific fragment area:
FragmentTransaction ft;
Fragment mFragment = new MySampleFragment();
currentFragment = mFragment;
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.myfragmentid, mFragment, "samplefragment");
ft.commit();
Here we inflated the fragment inside a specific area with an actual ID which is R.id.myfragmentid. In this case you can specify which area in your page you will show your desired output upon a specific events like button click, hover, etc.
Hope it helps. Cheers! :)