FragmentActivity Blackout Tab Screen

I am using an android sample, FragmentTabs .

Say there are 4 tabs (A, B, C, D). I am replacing a tab fragment (A) with another (E) using this code

FragmentTransaction transaction = getFragmentManager() .beginTransaction(); transaction.replace(R.id.realtabcontent, newFragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); 

In another C tab, when I press the back button, the tab screen (A) turns black.

+6
source share
3 answers

Hi Ashish, this problem occurs because the fragment can not understand how normal activity is in normal activity when you press a key while the recently used activity will automatically open, but in the fragment it will not happen when you press the back button , so we must support the opposite event in the fragment, here I use the code from which I process the activity of my fragment, so you can check this

 public void onListItemClick(ListView l, View v, int position, long id) { showDetail(position); } void showDetail(int position) { this.position=position; if(isDualPane){ getListView().setItemChecked(position, true); DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detail); if (detailFragment == null || detailFragment.getIndex() != position ) { detailFragment = new DetailFragment(position); FragmentTransaction ft =getFragmentManager().beginTransaction(); ft.replace(R.id.detail, detailFragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } } else { Intent intent =new Intent(); intent.setClass(getActivity(),DetailActivity.class); intent.putExtra("position", position); startActivity(intent); } 

see the else part shows that intent supports my snippets

please check out my example (Fragment With Gridview), which I have for all users of fragments, you will find something useful from it

check my answer at this link: How to show various layouts inside fragments

+2
source

Most likely you are adding the first fragment transaction to the back stack. When adding a transaction to the back stack, this means that when the user pushes back, the last transaction will be canceled. Therefore, if you added fragment A and deleted fragment B, then pressing back will delete fragment A and add fragment B (and restore some saved state for fragment B).

For your first transaction, you simply add the fragment to the Activity, so reversing will delete the fragment and thus will not display anything. So just donโ€™t add the first transaction to the back stack (suppose you did this ... I donโ€™t know, because you only inserted a small piece of code).

+1
source

I usually use the android sample, FragmentTabs , to manage tabs, and I have not experienced this problem. Without seeing the complete source code, it is hard to say for sure that this is happening. But if you do not use FragmentTabs , then you can try switching to it, since it has been fully tested;)

0
source

Source: https://habr.com/ru/post/917442/


All Articles