已经通过@Javascriptinterface在Fragment中收到了某个值,我想将此值传递给MainActivity以便对该变量执行某些操作。该片段应关闭。
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private WebView WebView;
private ProgressBar progressBar;
private Menu menu;
.......
.......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open , R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
final Menu navigationMenu = navigationView.getMenu();
navigationView.setNavigationItemSelectedListener(this);
WebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = WebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.supportMultipleWindows();
......
WebView.setWebViewClient(new MyWebViewClient());
WebView.loadUrl(webview_url);
WebView.setWebChromeClient(new MyWebChromeClient());
..............
}
public void getFragmentValue(String val ){
Toast.makeText(this, "open: "+val, Toast.LENGTH_LONG);
}
Fragment fragmentWeb = new MainFragment();
FragmentManager ft = getSupportFragmentManager();
public boolean checkMenuItemEnable = false;
public int TitleSize;
public int idItem;
@Override
public void onBackPressed() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
clearStack();
getMyData();
switch (dataFrag) {
case ("preference"):
idItem = R.id.action_settings;
break;
case ("bookmark"):
idItem = R.id.menu_bookmark;
break;
case ("myplaces"):
idItem = R.id.menu_added;
break;
}
MenuItem PfMenuItem = navigationView.getMenu().findItem(idItem);
PfMenuItem.setEnabled(true); <--- Thats no work
dataFrag = "none";
setHomeScreen();
}
}
private String dataFrag = "none";
public String getMyData() {
return dataFrag;
}
public void setHomeScreen() {
setTitleMain(R.string.app_name);
setLogoMain(android.R.drawable.list_selector_background);
setTitleSize(7);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (checkMenuItemEnable) {
if (Build.VERSION.SDK_INT >= 26) {
ft.beginTransaction().setReorderingAllowed(false);
}
ft.beginTransaction().detach(fragmentWeb).attach(fragmentWeb).commit();
} else {
ft.beginTransaction().replace(R.id.mainlayout, fragmentWeb, dataFrag).addToBackStack(null).commit();
}
checkMenuItemEnable = true;
item.setEnabled(false);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
MainFragment:
public class MainFragment extends Fragment implements OnBackPressed {
public MainFragment() {
}
private WebView WebView;
..........
..........
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView.findViewById(R.id.bugttracker_layout).setVisibility(ViewGroup.INVISIBLE);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
..........
..........
}
@SuppressLint("JavascriptInterface")
public class myWebAppInterface {
Context mContext;
myWebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void getValue(String val){
sentToActivity(val);
}
}
public void sentToActivity(String val) {
Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
getFragmentManager().popBackStack();
getActivity().onBackPressed();
((MainActivity) getActivity()).getFragmentValue(val);
}
}
使用正常的onBackPressed,一切正常,但是,一旦我尝试使用sendToActivity()以编程方式调用“返回”,我将无法再在菜单中找到并更改它。
尽管,如果挂起sendToActivity以执行一个按钮,请单击侦听器中的片段,那么一切都很好。
告诉我,我的错误在哪里?返回到主Activity时,为什么从@Javascriptinterface调用函数确实会删除对导航菜单项的访问?