I need to replace the snippet when retrieving the data. The data comes to @JavascriptInterface and calls the method.

@JavascriptInterface public void sayHello(String type) { CheatActivity.fTrans.replace(R.id.frgmCont, MainActivity.frag2).commit(); } 

Because In the main activation, the fragment is initially started, I get an error from the header. It is necessary to indicate each time the beginning of the transaction with a fragment

 fTrans = getSupportFragmentManager().beginTransaction(); 

But getSupportFragmentManager () gives an error when used in @JavascriptInterface

If I create a method in MainActivity that will change fragments when called from @JavascriptInterface, then the method must be made static. And getSupportFragmentManager () does not work in this case.

xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <FrameLayout android:id="@+id/frgmCont" android:layout_width="match_parent" android:layout_height="479dp"> </FrameLayout>... 

MainActivity

 public static Fragment1 frag1; public static Fragment2 frag2; public static FragmentTransaction fTrans; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cheat); frag1 = new Fragment1(); frag2 = new Fragment2(); ... fTrans = getSupportFragmentManager().beginTransaction(); fTrans.replace(R.id.frgmCont, frag1).commit(); 

UPD

 public class JsInterface { Context mContext; JsInterface(Context c) { mContext = c; } @JavascriptInterface public void sayHello(String type) { CheatActivity.fTrans = getSupportFragmentManager().beginTransaction(); CheatActivity.fTrans.replace(R.id.frgmCont, CheatActivity.frag2).commit(); } 
  • And what is the error when calling getSupportFragmentManager in JavascriptInterface? And where, in which class, is the latter described? - Yuriy SPb
  • @Yuriy SPb, made an update. I think that I am not correctly calling getSupportFragmentManager (). cannot resolve method - blatube.com 4:08 pm
  • In the JsInterface class, there is no access to the activation class and therefore there is no getSupportFragmentManager method. Try to make this class the inner class of the activit class. Or to mContext to the activit class (if it is an activit) and then call getSupportFragmentManager from it - YuriySPb

0