I'm trying to develop an application that contains only one Activity and two Fragments . Each Fragment can call the interface method implemented by the Activity :
@Override public void onFragmentRequest(final String fragmentTag) { new Thread(new Runnable() { @Override public void run() { try { // симулируем работу с сетью Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { Fragment fragment = getSupportFragmentManager() .findFragmentByTag(fragmentTag); if(fragment == null) Log.d(TAG, "fragment is null"); else { if(fragment instanceof OnFragmentResponse) ((OnFragmentResponse) fragment).onFragmentResponse(); } } }); } }).start(); } But the problem is that after rotating the screen (changing the configuration) after calling onFragmentRequest , getSupportFragmentManager().findFragmentByTag(fragmentTag) returns null
I replace all the fragments and add them to the back stack, and after all this I call the commit method, as well as manager.executePendingTransactions() , but this does not solve the problem
Logs before or without screen rotation:
02-07 16:39:12.202 29750-29750/com.testfrag3 D/MainActivity: onFragmentRequest: fragmentTag = NewsFragment 02-07 16:39:12.203 29750-29750/com.testfrag3 D/MainActivity: getSupportFragmentManager.entryCount = 1 02-07 16:39:12.203 29750-29750/com.testfrag3 D/NewsFragment: onFragmentResponse success Logs, when onFragmentRequest was called and a turnaround occurred:
02-07 16:39:23.454 29750-29750/com.testfrag3 D/MainActivity: onFragmentRequest: fragmentTag = NewsFragment 02-07 16:39:23.454 29750-29750/com.testfrag3 D/MainActivity: getSupportFragmentManager.entryCount = 1 02-07 16:39:23.454 29750-29750/com.testfrag3 D/MainActivity: fragment is null
getSupportFragmentManagerfrom the destroyed activation, but then somehow it doesn’t fit in my head: the activation is destroyed, but its context is not, and you can call its methods. I also thought thatrunOnUiThreadwill be called from the newly created activation, but it was not there) - Trancer