I added a fragment to my layout containing a web view and this works. Now I try to trigger an action within the javascript of the code to hide the fragment itself.
I implemented a class called WebAppInterface that can be called from Javascript and if I press the button hidePicker() is called. Now I try to hide the fragment, but nothing happens. This is my function:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class WebAppInterface extends FragmentActivity{
public void hidePicker(){
Log.d("CAPerm", "hide Picker");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
Fragment picker = fm.findFragmentById(R.id.registrationpickerfragment);
ft.hide(picker);
ft.commit();
Log.d("CAPerm", "hidden");
}
}
the fragment is displayed within a LinearLayout in a Scrollview in a ConstraintLayout.
I receive the following console output:
W/System.err: java.lang.IllegalStateException: Activity has been destroyed
W/System.err: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2087)
W/System.err: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
W/System.err: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
at com.mypackage.WebAppInterface.hidePicker(WebAppInterface.java:43)
at android.os.MessageQueue.nativePollOnce(Native Method)
W/System.err: at android.os.MessageQueue.next(MessageQueue.java:386)
at android.os.Looper.loop(Looper.java:169)
at android.os.HandlerThread.run(HandlerThread.java:65)
V/WindowManager: Exit animation finished in Window{fe0401a u0 com.mypackage.UserRegistration EXITING}: remove=true
E/WindowManager: win=Window{fe0401a u0 com.mypackage.UserRegistration EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true
V/WindowManager: postWindowRemoveCleanupLocked: Window{fe0401a u0 com.mypackage.UserRegistration}
Removing Window{fe0401a u0 com.mypackage.UserRegistration} from AppWindowToken{b12e137 token=Token{266fe36 ActivityRecord{37b83d1 u0 com.mypackage.UserRegistration t6381}}}
the later log message "hidden" is not executed and the error seems to happen in line 43(ft.commit())
Any ideas why the fragment isn't removed or some workarounds how to solve it better?
Thanks in advance :)
Regards Christian
I was finally able to solve my problem :)
I created a boolean variable with an event listener in the UI thread like this: Android: Listening to variable changes - Stack Overflow and called setBoo from within the WebAppInterface function :)