I'm working on an application and let's say I have 5 different layouts plus a main layout which has links to each layout. In the main layout when the user click to the first button it is suppose to go the related layout by sliding. And It's same for other views.
When the user goes to one of these layouts, the next layout also has a couple of links to other layouts as like a chain. And this chain is moving from layout to the other one by sliding.
I have tried to do this with view flipper but since I have so many different layouts and they have background images and some contents in it, I get out of memory error.
So I'm trying to find a solution in this manner. Any idea how to accomplish this ?
I would recommend you to use Jake Whartons ViewPageIndicator:
Create a Fragment for every of your Layouts and set the layout to your Fragment with the onCreateView() methode inside your fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout, container, false);
}
Now create a FragmentPagerAdapter there sould be a methode called getItem(). Switch the Position and set it to your Fragment:
public Fragment getItem(int position) {
switch(position)
{
case 0:
TestFragment fragment = new TestFragment();
return fragment;
case 1:
TestFragment2 fragment2 = new TestFragment2();
return fragment2;
}
DefaultFragment fragment3 = new DefaultFragment();
return fragment3;
}
Now you should be able to swipe to your Layouts(Fragments) easily
Start a new android.app.Activity hosting each layout and override the transitions between activities using overridePendingTransition(R.anim.animation_leave, R.anim.animation_enter); where *.anim.animation_leave and _enter are neatly already spec'ed out for you in this SO answer.
You can write the overridePendingTransition code immediately after your call to start the 'chained' activity and (if required) the reverse effect can be applied by providing the opposing transitions after calling finish() on the Activity or selecting the 'Up' navigation button press or 'back-button' press.
Having a single activity manage one layout will (in my experience) also benefit code maintainability.
Related
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 am adding layouts to my Project and each time I add a layout it also add another layout that comes with the Word "fragment"... can somebody explain me for what is for? I had look over the web and it explain other kind of fragments...
Android Studio, when asked to create an Activity, will create 4 things for you :
An Activity class
a layout file for the Activity class, which will include a FrameLayout serving as the container to place the fragment
a Fragment class (created as an innner class inside your Activity)
a layout file for your fragment (this is the second layout you see in
your project structure), say for example fragment_test.xml
If you look closely, you Activity code will contain something like this :
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
return rootView;
}
}
My guess it that this was done so it guides developers to use fragments for the screen's actual content rather than placing it inside the Activity's layout itself. The Fragment is designed to be a re-usable component, so if you have several layouts for your activity depending on the screen size/orientation, you can re-use the same fragments, just placing them differently inside your Activity layout, which is an excellent practice.
I hope I clarified things a bit ;)
This is an Android 0.8 question, using the Activity with Fragment template.
So, how would you go about swapping one fragment in for a second fragment? in the same Frame? Perhaps for a button click, for example.
Use case, might be a "connect the dots" questionnaire where the next button goes to the next fragment.
I understand that the answer is FragmentManager and FragmentTransactions.
When I do this from with in a click event,
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.replace(R.id.container, new FRAG02());
FT.addToBackStack(null);
FT.commit();
I get an error:
must implement OnFragmentInteractionListener
It would seem that there is a SOP way of replacing fragments that I am not aware of. Seems like a related comment.
this is bit when i'm creating view,
public static int a=0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
a++;
View view = inflater.inflate(R.layout.fragment_layout, container, false);
editText = (EditText) view.findViewById(R.id.amount);
TextView tv = (TextView) view.findViewById(R.id.textView1);
Button button = (Button) view.findViewById(R.id.addTransaction);
button.setOnClickListener(this); ///fragments implements OnClickListener
editText.setText(""+a);
tv.setText(""+a);
return view;
}
when i load this fragment first time, my editText is empty, but when i load fragment again, value in the editText is same like in previous execution.
Is anyone has idea what i'm doing wrong? And how i can fix it?
** EDIT
i modified little bit my code, now each time when i'm loading fragment a is incremented. and io noticed weird behaviour. tv has has actual value of a, while editText still has old value
You need to move the settext ( and probably some other stuff) into a later method in the fragment life cycle e.g. onActivityCreated.
Before adding the fragment try to get the fragment by calling FindFragmentByTag("tag"), i think you are adding new fragments on top of each other. Also add your fragment transaction to back state and then to check if more than one fragments are added, press back button. I had similar problem, and the reason was i kept adding new fragments in activity
Fragments are tied to activities, so my guess is that the activity is not being destroyed, which means your fragment is not being destroyed and instead "resumed". Perhaps you want that code in the onResume callback? Take a look at the Fragment lifecycle: Android Fragments
this is a though one for me. I have a MainActiviy which extends FragmentAnctivity. There I have 1 FrameLayout and buttons below to change frame's content. I do so by switching show/hide for created fragments which I added to FrameLayout before in OnCreate.
I'm also nesting more fragments in 1 fragment (As I have 1 fragment for 1 type of content and inside of it there is listFragment which is changed to DetailFragment after OnItemClick... again with show/hide approach).
Problem is that in 2 different contents I have 2 different instances of 1 Fragment class, so those 2 instances use 1 same layout file. And although the first of those fragment is hidden and 2nd is shown, when I change some view through 2nd instance then layout of 1st instance is changed and 2nd remains same as before. (Hope it is understandable)
I guess it's totally a mistake in managing and understanding of fragments' lifecycle, so can please someone help me to solve this?
Thanks very much :)
I suppose you get main point of fragments using practices. Your problem is simple. I almost sure you use getActivity().findViewById(...) calls to access views in your Fragment (or nested Fragment whatever). I this case Activity would return you fist view with defined id from whole your views hierarchy.
Solution is pretty simple - you just must avoid getActivity().findViewById(...) construction and get all links to views in onCreateView() callback and use exact this link with all future operations. Than everything will be ok. Here is simple example:
private TextView mDummyText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout_name, container, false);
initMembersViews(v);
return v;
}
private void initMembersViews(View v) {
mDummyText = (TextView) v.findViewById(R.id.fr_houses_list_text);
}
Hope it would helps you! Good luck!
I am trying to understand a bad behaviour in fragments: the onCreateView and onActivityCreated methods are called even the fragment is not 'visible' in the layout.
If you use the code:
TestFragment testFragment = new TestFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentDetail, testFragment, "test");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
replacing the FrameLayout with id fragmentDetail with the fragment and then you rotate the device, the fragments method are still invoked even if the container is not present anymore in the portrait layout. This doesn't happen if you use the 'static' <fragment> tag.
If you use the static fragment, the fragments methods are invoked just when the fragment appears. Is it possible to achieve the same behaviour without using the fragment tag? I need a way to avoid the rendering of the fragment if it is not in the layout.
Thanks
I have found one fix to this. It is slightly different from the suggested Handling orientation changes with Fragments one:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (!fragment.isInLayout() && container == null) return null;
...
}
In this way you can avoid the case when the fragment is statically put into the layout (in that case the container is null but the method isInLayout() returns true.
By the way it is still weird to me this behaviour.
AFAIK, fragments work almost as Activities. They have the same lifecycle. http://developer.android.com/reference/android/app/Fragment.html#Lifecycle So, if you don't have references to them, it won't make them close. They are referenced by the system and live by themselves. So, you should finish them somehow.