Replace fragment with another fragment inside ViewPager

Try using FragmentStatePagerAdapter http://android-developers.blogspot.com/2011_08_01_archive.html coz it will remove the fragment as you swipe through the view(also when you call getitem), so you don’t need to explicitly remove them yourself. I’ve been having the same problem it works for me. I try your code and remove the if (fragment!=null) block and it works.

Fragment standard transition not animating

I finally got this to work after much trial and error. First and foremost, get the lastest ACL, it did fix custom animations, and while this was not my exact problem, once those worked I ended up using them instead of standard transitions. Right now I am using: ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out); The key to making it work … Read more

How can I switch between two fragments, without recreating the fragments each time?

After @meredrica pointed out that replace() destroys the fragments, I went back through the FragmentManager documentation. This is the solution I’ve come up with, that seems to be working. /* The click listener for ListView in the navigation drawer */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, … Read more

Android Fragment onCreateView vs. onActivityCreated

If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you – for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so. Also accessing the view hierarchy … Read more

How to hide the soft keyboard inside a fragment?

As long as your Fragment creates a View, you can use the IBinder (window token) from that view after it has been attached. For example, you can override onActivityCreated in your Fragment: @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); }

How to implement onBackPressed() & intents in fragment?

You can interact with the fragment using a callback interface. In your activity add the following: public class MyActivity extends Activity { protected OnBackPressedListener onBackPressedListener; public interface OnBackPressedListener { void doBack(); } public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) { this.onBackPressedListener = onBackPressedListener; } @Override public void onBackPressed() { if (onBackPressedListener != null) onBackPressedListener.doBack(); else super.onBackPressed(); } @Override … Read more

Stop fragment refresh in bottom nav using navhost

Try this: public class MainActivity extends AppCompatActivity { final Fragment fragment1 = new HomeFragment(); final Fragment fragment2 = new DashboardFragment(); final Fragment fragment3 = new NotificationsFragment(); final FragmentManager fm = getSupportFragmentManager(); Fragment active = fragment1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); … Read more