OnClickListener仅激活一次

时间:2018-09-27 23:16:20

标签: java android

因此,我试图允许用户在失去连接时单击“重新加载”按钮,以尝试重新创建活动并再次与Web视图链接,以下代码有效,但仅当连接为如果先单击后还原,然后单击失败,然后再次单击,则不会恢复该活动。有什么想法吗?

package online.lhamaths.lornshillmathsonline;    
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private ImageView imageView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (isNetworkAvailable()) {
            button = findViewById(R.id.button);
            imageView = findViewById(R.id.imageview);
            webView = findViewById(R.id.webview);
            createWebView(webView);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Restarting", Toast.LENGTH_SHORT).show();
                    finish();
                    startActivity(getIntent());
                }
            });
        } else {
            Toast.makeText(getApplicationContext(), "No Connection Present", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    public void createWebView(WebView webView) {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https:lhamaths.online");
        Toast.makeText(getApplicationContext(), "Loading Site", Toast.LENGTH_SHORT).show();
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.endsWith(".pdf")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    Toast.makeText(getApplicationContext(), "Opening File", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }

            public void onReceivedError(WebView webView, int errorCode, String description, String failingURL) {
                webView.destroy();
                button.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
                Toast.makeText(getApplicationContext(), "No Connection Present, please reload the app", Toast.LENGTH_SHORT).show();
            }

            public void onPageFinished(WebView webView, String url) {
                imageView.setVisibility(View.INVISIBLE);
                button.setVisibility(View.INVISIBLE);
            }
        });
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
    }

}

1 个答案:

答案 0 :(得分:0)

这是因为要在onClickListener条件内设置isNetworkAvailable()。因此,当您重新创建活动并且没有Internet连接时,不会执行if条件。因此onClickListener不会第二次被设置。尝试将setOnClickListener代码置于if条件之外。