你好,我是android的初学者。 我得到了一些片段。当我点击我的android的后退按钮时,我想回去。 我的片段(在我的ActualitesFragment扩展片段中):
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Boolean isConnected;
View rootView = inflater.inflate(R.layout.fragment_actualites, container, false);
MainActivity.webView = (WebView) rootView.findViewById(R.id.webView2);
linearLayout = (LinearLayout) rootView.findViewById(R.id.progress_bar);
InternetState internetState = new InternetState();
isConnected = internetState.isConnected(getActivity().getApplicationContext());
try
{
if(isConnected)
{
GetHtmlCode task = new GetHtmlCode();
task.setLinearLayout(linearLayout);
task.checkWebView(MainActivity.webView);
task.execute("myURL");
} else
{
Toast.makeText(getActivity().getApplicationContext(), "Oups, no internet", Toast.LENGTH_LONG).show();
}
}catch (Exception ex)
{
Toast.makeText(getActivity().getApplicationContext(), "Oups, no internet", Toast.LENGTH_LONG).show();
}
return rootView;
}
我的MainActivity with onBackPressed()(我尝试了很多解决方案但是,它不起作用)
@Override
public void onBackPressed()
{
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof ActualitesFragment){
boolean goback = webView.canGoBack();
if(!goback)
super.onBackPressed();
}
// super.onBackPressed();
/* if (webView.canGoBack()) {
webView.goBack();
}else{
super.onBackPressed();
}*/
}