ViewPager and fragments — what’s the right way to store fragment’s state?

When the FragmentPagerAdapter adds a fragment to the FragmentManager, it uses a special tag based on the particular position that the fragment will be placed. FragmentPagerAdapter.getItem(int position) is only called when a fragment for that position does not exist. After rotating, Android will notice that it already created/saved a fragment for this particular position and … Read more

How do I get the currently displayed fragment?

When you add the fragment in your transaction you should use a tag. fragTrans.replace(android.R.id.content, myFragment, “MY_FRAGMENT”); …and later if you want to check if the fragment is visible: MyFragment myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag(“MY_FRAGMENT”); if (myFragment != null && myFragment.isVisible()) { // add your code here } See also http://developer.android.com/reference/android/app/Fragment.html

How to correctly save instance state of Fragments in back stack?

To correctly save the instance state of Fragment you should do the following: 1. In the fragment, save instance state by overriding onSaveInstanceState() and restore in onActivityCreated(): class MyFragment extends Fragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); … if (savedInstanceState != null) { //Restore the fragment’s state here } } … @Override public … Read more

IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

Please check my answer here. Basically I just had to : @Override protected void onSaveInstanceState(Bundle outState) { //No call for super(). Bug on API Level > 11. } Don’t make the call to super() on the saveInstanceState method. This was messing things up… This is a known bug in the support package. If you need … Read more

How to implement onBackPressed() in Fragments?

I solved in this way override onBackPressed in the Activity. All the FragmentTransaction are addToBackStack before commit: @Override public void onBackPressed() { int count = getSupportFragmentManager().getBackStackEntryCount(); if (count == 0) { super.onBackPressed(); //additional code } else { getSupportFragmentManager().popBackStack(); } }

Why fragments, and when to use fragments instead of activities?

#1 & #2 what are the purposes of using a fragment & what are the advantages and disadvantages of using fragments compared to using activities/views/layouts? Fragments are Android’s solution to creating reusable user interfaces. You can achieve some of the same things using activities and layouts (for example by using includes). However; fragments are wired … Read more

How to determine when Fragment becomes visible in ViewPager

How to determine when Fragment becomes visible in ViewPager You can do the following by overriding setUserVisibleHint in your Fragment: public class MyFragment extends Fragment { @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { } else { } } }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)