在使用片段的变量时在活动中添加后退按钮支持

时间:2015-07-14 14:27:30

标签: android android-fragments webview

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Fragment_view frag=new Fragment_view();
    FragmentManager manager=getFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    transaction.add(R.id.fragment,frag,"hey");
    transaction.commit();
}
@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
 if (keyCode == KeyEvent.KEYCODE_BACK) {

 if (inCustomView()) {
 hideCustomView();
 return true;
 }
 if ((mCustomView == null) && webView.canGoBack()) {
 webView.goBack();
 return true;
 }
 }
 return super.onKeyDown(keyCode, event);
 }

现在,像inCustomView和hidecustomView这样的变量在另一个片段中定义。因此,在使用片段中的变量时,如何在活动上添加此后退按钮支持。所以我基本上想在Android活动中实现后退按钮支持,同时使用片段中定义的变量。

public class Fragment_view extends Fragment{

private WebView webView;
private FrameLayout customViewContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private View mCustomView;
private myWebChromeClient mWebChromeClient;
private myWebViewClient mWebViewClient;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view= inflater.inflate(R.layout.fragment_view, container,false);
    customViewContainer = (FrameLayout)view.findViewById(R.id.customViewContainer);
        webView = (WebView)view. findViewById(R.id.webView);

        mWebViewClient = new myWebViewClient();
        webView.setWebViewClient(mWebViewClient);

        mWebChromeClient = new myWebChromeClient();
        webView.setWebChromeClient(mWebChromeClient);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSaveFormData(true);
        String htmlFile = "file:///" + Environment.getExternalStorageDirectory()
                .getPath()
                + "/video_nsta/index.html";
        webView.loadUrl(htmlFile);

    return view;
}


public boolean inCustomView() {
    return (mCustomView != null);
}

public void hideCustomView() {
    mWebChromeClient.onHideCustomView();
}


@Override
public void onPause() {
    super.onPause();    
    webView.onPause();
}

@Override
public void onResume() {
    super.onResume();    
    webView.onResume();
}

@Override
public void onStop() {
    super.onStop();    
    if (inCustomView()) {
        hideCustomView();
    }
}





class myWebChromeClient extends WebChromeClient {

    @Override
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {

        onShowCustomView(view, callback);    
    }

    @Override
    public void onShowCustomView(View view,CustomViewCallback callback) {


        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        mCustomView = view;
        webView.setVisibility(View.GONE);
        customViewContainer.setVisibility(View.VISIBLE);
                 customViewContainer.addView(view);
        customViewCallback = callback;

    }



    @Override
    public void onHideCustomView() {
        super.onHideCustomView();    

        if (mCustomView == null)
            return;
        webView.setVisibility(View.VISIBLE);
       customViewContainer.setVisibility(View.GONE);
        mCustomView.setVisibility(View.GONE);
        customViewContainer.removeView(mCustomView);
        customViewCallback.onCustomViewHidden();

        mCustomView = null;
    }
}

class myWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);   
    }
}

}

1 个答案:

答案 0 :(得分:0)

这是正确分发后退按钮的问题。

首先创建一个可以处理它的片段:

public abstract class BackButtonFragment extends Fragment{
   public boolean onBackPressed(){
        return false; // defaults to false
   }
}

然后您将所有片段更改为从BackButtonFragment扩展。

然后在您的活动中,您将使用适当的回拨功能(从您的活动中删除onKeyDown并使用此代码):

@Override public void onBackPressed() {

    Fragment f = getFragmentManager().findFragmentById(R.id.fragment);
    if(f instanceof BackButtonFragment){
        if(((BackButtonFragment)f).onBackPressed()) 
            return;
    }

    // if fragment didn't use back button do the normal stuff
    super.onBackPressed();
}

然后在每个片段中override使用方法onBackPressed并根据您的需要返回正确的值truefalse