Gezinme çekmecesi var ve üzerlerine tıklayın/gizler/tam ekran parçaları oluşturur.fragmanları, bazen üst üste binmiş durumda - Android
Çoğunlukla bu kod işe yarıyor. Ama bazen, belki de% 1, ben zaten çalışıyor iken uygulamayı açarken çılgın tam ekran parçası çılgın olacak.
Sorun benim kodumla ..? Ya da belki de Android'de fark edilmeyen başka bir şey zaten oluşturulmuş etiketlerle parçalarım var mı?
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Get to drawer layout so we can interact with it
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Get the fragment manager to remove/add fragments
FragmentManager fragmentManager = getSupportFragmentManager();
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
// Hide visible fragment
fragmentManager.beginTransaction().hide(getVisibleFragment()).commit();
// Check if the fragment exists first.
if(fragmentManager.findFragmentByTag("profileFragment") != null) {
// If the fragment exists, show it (no reason to recreate it).
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag("profileFragment"))
.commit();
} else {
// If the fragment does not exist, add it to fragment manager with a tag to identify it.
// Create new fragment instance with required argument(s).
ProfileFragment fragment = ProfileFragment.newInstance();
fragmentManager.beginTransaction()
.add(R.id.content_frame, fragment, "profileFragment")
.commit();
}
// Set the title
mToolbarTitleTextView.setText(R.string.title_activity_profile);
} else if (id == R.id.nav_feed) {
// Hide visible fragment
fragmentManager.beginTransaction().hide(getVisibleFragment()).commit();
// Check if the fragment exists first.
if(fragmentManager.findFragmentByTag("feedFragment") != null) {
// If the fragment exists, show it (no reason to recreate it).
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag("feedFragment"))
.commit();
} else {
// If the fragment does not exist, add it to fragment manager with a tag to identify it.
fragmentManager.beginTransaction()
.add(R.id.content_frame, new feedFragment(), "feedFragment")
.commit();
}
// Set the title
mToolbarTitleTextView.setText(R.string.title_activity_feed);
} else if (id == R.id.nav_notifications) {
// Hide visible fragment
fragmentManager.beginTransaction().hide(getVisibleFragment()).commit();
// Hide the post button
mPostButton.setVisibility(View.GONE);
// Check if the fragment exists first.
if(fragmentManager.findFragmentByTag("notificationsFragment") != null) {
// If the fragment exists, show it (no reason to recreate it).
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag("notificationsFragment"))
.commit();
} else {
// If the fragment does not exist, add it to fragment manager with a tag to identify it.
fragmentManager.beginTransaction()
.add(R.id.content_frame, new NotificationsFragment(), "notificationsFragment")
.commit();
}
// Set the title
mToolbarTitleTextView.setText(R.string.title_activity_notifications);
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
// Useful method to hide the currently visible fragment
public Fragment getVisibleFragment(){
FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if(fragments != null){
for(Fragment fragment : fragments){
if(fragment != null && fragment.isVisible())
return fragment;
}
}
return null;
}
DÜZENLEME:
Burada/gizlemek/parçalarını oluşturmak göstermek nasıl alakalı kodudur hata ayıklamak zor hale getirir Bu hata yeniden gerçekten zor. Rastgele gerçekleşiyor gibi görünüyor.
=. Haklısınız, parça durumunu (daha fazla güçlük) depolamak için yaşam döngüsü yöntemlerini kullanabilirim. Bunun bir hafıza sorunu olduğunu mu söylüyorsun ..? – Micro
'FragmentTransaction.add()' işlevini kullanarak, bir parça yığını oluşturuyorsunuz ve yalnızca bir üstte görüntüleniyorsunuz (daha sonra sırasıyla 'show()' veya 'hide()' kullanarak değiştiriyorsunuz. replace() 'bir seferde sadece bir parçayı tutar.Tek parçanın bir örneğinin aynı anda aktif olmasıyla çakışan parçanın önüne geçebilirsiniz. –