Reputation: 400
I am developing a kiosk app so it's takes over the tablet. Single activity with bunch of different fragments. I commit a fragment in the oncreate of the activity. Then when the device is connected to bluetooth it will load a new fragment by removing the old one and adding a new one instead (calls a function cancelFunc()). The issue that I am having is that the first fragment is still visible after removal. My logs show that it is actually removed and a new fragment is attached instead but I can't see the new one or any other that are attached later. However, when I tap the screen it shows the new fragment. A bit strange and I wonder what I am doing wrong. It is something that only happens on the fragment attached on the oncreate of the activity. The code is rather long so I'm just showing you a few important parts. Thanks in advance for your help.
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.backbone);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.add(R.id.fragmentContainer, new SystemDownFragment(), "SystemDownFragment");
ft.commit();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Log.d(TAG, "adapter is null");
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
public void cancelFunc() {
Log.d(TAG, "cancelFunc()");
removeFrag();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.add(R.id.fragmentContainer, new StartupFragment(), "StartupFragment");
ft.commit();
getFragmentManager().executePendingTransactions();
}
private void removeFrag(){
Log.d(TAG, "removeFrag()" + getActiveFragments().size());
if(getActiveFragments().size() > 0) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
for (int i = 0; i < getActiveFragments().size(); i++) {
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.remove(getActiveFragments().get(i));
}
ft.commit();
}
}
@Override
public void onAttachFragment (Fragment fragment) {
Log.d(TAG, "onAttachFragment");
fragList.add(new WeakReference(fragment));
}
public List<Fragment> getActiveFragments() {
ArrayList<Fragment> ret = new ArrayList<Fragment>();
for(WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
if(f != null) {
if(f.isVisible()) {
ret.add(f);
}
}
}
return ret;
}
My logged data shows the following:
MainActivity: onCreate()
MainActivity: onAttachFragment
MainActivity: onStart()
MainActivity: onResume()
MainActivity: Bluetooth Connected
MainActivity: cancelFunc()
MainActivity: removeFrag()1
MainActivity: onAttachFragment
Upvotes: 3
Views: 2073
Reputation: 774
You have to replace new fragment when back pressed in that activity .
public void onBackPressed() {
ft.replace(R.id.fragment_container, new_fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
Upvotes: 0
Reputation: 3294
It might solve your problem but it also good practice for what you want to do; Use ft.replace(...)
on your FragmentTransaction instead of adding and removing...
Upvotes: 0
Reputation: 774
In order to remove fragment you just need to call activity onBackPressed() method, since this method perform operation of going back thus it will remove your current fragment from activity, and then you can add another one. So update your removefrag() method with below code.
try this,
void removeFrag(){
onBackPresssed();
}
Upvotes: 0
Reputation: 105
Clear or remove all views from the current fragment, and then replace new fragment in Layout.
Like this one, try this
myLayout.removeAllViews();
AddNewFragment new = new AddNewFragment();
this.getFragmentManager().beginTransaction().replace(R.id.fragment_mainLayout, new, "").addToBackStack(null).commit();
Upvotes: 2