I have a few fragments in PageAdapter which placed on other fragment and I need somehow to control their states, update some views, etc.
I have code which looks like below:
Fragment frg1;
Fragment frg2;
frg1.setHandleProtocol(this);
class MyAdapter extends FragmentPageAdapter {
public Fragment getItem(int pos) {
return frg1;
}
}
This is very simple snippet, but it should help me to explain. This approach works good by the moment when Activity will be killed and restored by Android. In that case PageAdapter re-creates new fragments by Object class and they won't have a old links and depends.
Is there any good way to handle this new fragments?
I have ugly workaround. I am just going through all fragments in FragmentManager and re-assigning their to the old links.
Related
I have to learn, but for YOU it is simple:
I created a simple basic NavigationDrawer-app with Android Studio (the offered NavigationDrawer you can choose when creating a new app).
My question: How (or where) can I put my GoogleMap-fragment? Can I replace the "PlaceholderFragment" or do I create a new Fragment? And how to implement?
May I ask for a LITTLE and EASY example or hint? As I said, just an absolute simple running NavigationDrawer/GoogleMap app. I know a lot about GoogleMap, but I don't get the fusion.
Thanks for any little help and best regards from Germany.
you can replace the PlaceHolderFragment generated with your own Fragment, be it a regular fragment or a GoogleMap fragment.
The private PlaceHolderFragment class is created for your convenience, but you could just create a separate Fragment class and remove the PlaceHolderFragment class. You would open your map fragment instead of the PlaceHolderFragment inside the method onNavigationDrawerItemSelected in the Activity created with the project template.
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, MyMapFragment.newInstance(position + 1)) // instead of PHF
.commit();
}
Hope this helps.
I'm new to fragments. I have an activity that extneds FragmentActivity and uses the ViewPager to swipe between fragments.
Within one of the fragments I want to launch a new fragment that is "outside" of whats in the view pager. Is it possible to add a fragment like this or do I need to start a new activity with the fragment.
Now I have it launch a new activity but it is very slow.
Activity A
Fragment A
Fragment B
Fragment C
Activity B
Fragment D
Fragment A
So fragment A can launch acitvity B to get to fragment D but ideally it would be cool if I could just inject fragment D in Activity A
Hope that makes sense.
Yes, you can, but I strongly suggest you avoid it, unless you have a good reason to do so.
The ViewPager is (I assume) hosted in your Activity. The ViewPager obtains its views from its adapter. (A FragmentStateAdapter or similar). So you could tell the activity to change its layout (and or hide the Viewpager and show another FrameLayout) or you can simply launch another Activity that contains a single fragment. This is also fine.
Messing with the ViewPager/Adapter is usually complicated and you may waste time trying to make it work.
On the other hand, you could add the Fragment to the ViewPager's Adapter and use the getType of the adapter to return a different type of Fragment.
So you could do (pseudo code):
mPagerAdapter.addFragmentDToTheDataAtPositionZero(); //longFancyName ;)
mPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(0, false);
that'd add Fragment D to the "top" of the viewpager and will switch to it.
Your Adapter then has to use an Interface to determine what type of fragment must be instantiated…
The getItem of the adapter would look like… (again, pseudocode)
#Override
public Fragment getItem(final int i) {
final FragmentTypeInterface f = yourData.get(i);
if (f.isD()) {
return FragmentD.newInstance();
} else {
return OtherFragment.newInstance();
}
}
And so forth… :)
I've searched hours for examples of multiple intents and Fragments within ActionBarActivity. I am learning android programming from Deitel series and other well informative resources. The obstacles which I'm facing are to start with a simple "Hello World" program from two intents using Fragments, but the resources I'm learning from doesn't contain examples extending ActionBarActivity but they all extend Activity.
The roadblock for me is inflating fragments from ActionBarActivity using its available methods. If i can acheive this simple task, my learning curve would excel immenesly.
ActionBarActivity extends FragmentActivity, which extends Activity. So, everything that Activity does, ActionBarActivity does it too.
ActionBarActivity provides access and control for the ActionBar, while FragmentActivity provides a FragmentManager, that is used to manage your Fragments.
ActionBarActivity provides you all these features.
There are lots of examples on the web and this site is the right place to start.
Here is what I think you don't understand.
1. ActionBarActivity vs Activity
Don't worry about ActionBarActivity vs Activity. ActionBarActivity extends Activity so all the code that is valid in Activity is present in AcctionBarActivity. Please check this link.
2. Inflation
Usually what I do is make a separate class for each of my fragments as such,
public class Fragment1 extends Fragment{
// in the onCreateView I create a rootView & inflate.
final View rootview = inflater.inflate(R.layout.your_layout_file, container,false);
// Now you can use rootView to call all your Activity functions. Such as findView
//onClick etc.
return rootview;
3. In the MainActivity
This purely depends on the way you want to structure your app. But certainly you must have a getCount and getItem function. Like so.
#Override
public Fragment getItem(int i){
switch (i) {
case 0:
return new Fragment1();
case 2:
return new Fragment2();
}
}
#Override
public int getCount() {
return 2;
}
I'm writing an application that uses the FragmentPagerAdapter. The fragments in the adapter need to be updated according to outside data - but that doesn't happen. I noticed that the fragment classes are only instantiated once, in the getItem function:
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(),
info.args);
}
Even if I delete the class and use a new one, nothing helps - this method is only called once, the first time that the tab is populated, and then never again. Anyone has an idea why?
Thanks!
getItem will be called whenever the adapter needs a fragment and the fragment does not exist.
If the fragment already exists in the FragmentManager then there is no need to instantiate it and getItem does not need to be called.
To update an existing fragment you would need to retrieve it from the FragmentManager or the adapter and manipulate it accordingly.
By default, the viewpager will create fragments for the visible page and the one next to it. I.e to start with, fragments in position 1 and 2. When you swipe to page 2, the fragment at position 3 will be created etc
To be more specific than the answer above (which is correct!), getItem is called by FragmentPagerAdapter's instantiateItem(ViewGroup container, int position) method. Just in case that helps :)
Simply use FragmentStatePagerAdapter instead of FragmentPagerAdapter
FragmentStatePagerAdapter destroys the instance of unneeded instance of fragments and instantiates again on-demand basis. On the other hand, FragmentPagerAdapter just detach the Fragment and reattach it. So fragments created in FragmentPagerAdapter never get destroyed. That's why I always prefer FragmentStatePagerAdapter.
I'm working with fragments with ViewPager concept. I'm creating a diary app in which I'm using only one fragment which gets all updates from database and show in it. Until this everything is fine...According to my requirement when i click on a button in fragment i need to show another fragment which allows the user to store the images.
My problem is.....
--If i use replace method in fragments it was not replacing properly in the sense if A is fragment which consists of viewpager and B is a fragment i want to replace.
--Then if i use replace B is visible but A also appears under the fragment B
FragmentManager m=getActivity().getSupportFragmentManager();
FragmentTransaction ft = getActivity().getSupportFragmentManager()
.beginTransaction();
Demobutton demobutton = new Demobutton();
ft.replace(R.id.lay, demobutton);
ft.commit();
Hope you guys understand my problem. If you feel my question is incomplete please let me know that.
I have two suggestions depending on how you use the DemoButton Fragment:
1) Maybe your issue is with nested fragments. You get the FragmentManager from the activity but if the Demobutton is already part of an fragment use getChildFragmentManager() of the outer fragment instead.
2) From my experience when using a ViewPager with Fragments the PagerAdapter of the ViewPager should do all the fragment transactions. You could extend and overwrite the class FragmentPagerAdapter from the support library in order to get the correct fragment in your ViewPager when you need it.
I've developed a small example app that achieves this without overwriting native classes.
The point is to use a fragment as a container.
In your ViewPagerAdapter:
#Override
public Fragment getItem(int position) {
/*
* IMPORTANT: This is the point. We create a RootFragment acting as
* a container for other fragments
*/
if (position == 0)
return new RootFragment();
else
return new StaticFragment();
}
RootFragment layout should look like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root_frame" >
</FrameLayout>
You could review my complete explanation here: https://stackoverflow.com/a/21453571/1631136