android-fragmentactivity
3 android fragments in viewpager, how to keep them all alive?
use setOffscreenPageLimit(int limit) on the ViewPager object. From the documentation: [It set]s the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
commitAllowingStateLoss() in fragment activities
If I understand correctly you mean : Is there any reason NOT to indiscriminately do this without re-evaluating each individual case where I use a fragment? The answer is Yes – you should not do this without carefully re-evaluating each individual case where you use a fragment. Of course, by preventing restarts due to config … Read more
commitAllowingStateLoss() in fragment activities
If I understand correctly you mean : Is there any reason NOT to indiscriminately do this without re-evaluating each individual case where I use a fragment? The answer is Yes – you should not do this without carefully re-evaluating each individual case where you use a fragment. Of course, by preventing restarts due to config … Read more
Android check null or empty string in Android
Use TextUtils.isEmpty( someString ) String myString = null; if (TextUtils.isEmpty(myString)) { return; // or break, continue, throw } // myString is neither null nor empty if this point is reached Log.i(“TAG”, myString); Notes The documentation states that both null and zero length are checked for. No need to reinvent the wheel here. A good practice … Read more
After the rotate, onCreate() Fragment is called before onCreate() FragmentActivity
You should not count on a valid Activity until the onActivityCreated() call in the Fragment’s life cycle. Called when the fragment’s activity has been created and this fragment’s view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. The exact reasons … Read more
Manage toolbar’s navigation and back button from fragment in android
Add a toolbar to your xml <android.support.v7.widget.Toolbar android:id=”@+id/toolbar” android:layout_width=”match_parent” android:layout_height=”?attr/actionBarSize” android:background=”?attr/colorPrimary” android:theme=”@style/ThemeOverlay.AppCompat.ActionBar” app:popupTheme=”@style/ThemeOverlay.AppCompat.Light”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Fragment title”/> </android.support.v7.widget.Toolbar> Then inside your onCreateView method in the Fragment: Toolbar toolbar = view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.ic_back_button); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().onBackPressed(); } });
How to get application object into fragment class
Use appCtx = (UnityMobileApp) getActivity().getApplication(); in your fragment.
Hide/Show Action Bar Option Menu Item for different fragments
Try this… You don’t need to override onCreateOptionsMenu() in your Fragment class again. Menu items visibility can be changed by overriding onPrepareOptionsMenu() method available in Fragment class. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onPrepareOptionsMenu(Menu menu) { menu.findItem(R.id.action_search).setVisible(false); super.onPrepareOptionsMenu(menu); }