0

每當我刷新刷新,我的應用程序重新加載到主頁,而不是重新加載相同的網頁。我試圖用各種方法來檢測這種行爲,但找不到任何東西。你能說出這裏有什麼問題嗎?如何在每次刷卡刷新後停止WebView應用去首頁?

刷卡刷新應該只是重新刷新當前的網頁,而不是整個應用程序的權利?

MainActivity.java

package com.yoalfaaz.yoalfaaz; 

     import android.content.Intent; 
     import android.os.Bundle; 
     import android.support.design.widget.FloatingActionButton; 
     import android.support.design.widget.Snackbar; 
     import android.support.v4.widget.SwipeRefreshLayout; 
     import android.support.v7.app.AppCompatActivity; 
     import android.support.v7.widget.ShareActionProvider; 
     import android.support.v7.widget.Toolbar; 
     import android.view.View; 
     import android.view.Menu; 
     import android.view.MenuItem; 
     import android.webkit.WebSettings; 
     import android.webkit.WebView; 
     import android.webkit.WebViewClient; 
     import android.support.v4.view.MenuItemCompat; 

public class MainActivity extends AppCompatActivity { 
    private WebView YoWeb; 
    private ShareActionProvider mShareActionProvider; 

    SwipeRefreshLayout swipe; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     swipe = (SwipeRefreshLayout) findViewById(R.id.swiperefresh); 
     swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       LoadWeb(); 
      } 
     }); 
     LoadWeb(); 
    } 

    public void LoadWeb() { 
     YoWeb = (WebView)findViewById(R.id.webview); 
     WebSettings webSettings = YoWeb.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     YoWeb.loadUrl("https://www.yoalfaaz.com"); 
     swipe.setRefreshing(true); 
     YoWeb.setWebViewClient(new WebViewClient() { 

      //onPageFinished Method 
      public void onPageFinished(WebView view, String url) { 
       //Hide the SwipeRefreshLayout 
       swipe.setRefreshing(false); 
      } 
     }); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     switch (item.getItemId()) { 
      case R.id.action_settings: 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate menu resource file. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 

     // Locate MenuItem with ShareActionProvider 
     MenuItem item = menu.findItem(R.id.menu_item_share); 

     // Fetch and store ShareActionProvider 
     mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item); 

     // Return true to display menu 
     return true; 
    } 

    // Call to update the share intent 
    private void setShareIntent(Intent shareIntent) { 
     if (mShareActionProvider != null) { 
      mShareActionProvider.setShareIntent(shareIntent); 
     } 
    } 

    //private Intent setShareIntent() { 
    // Intent shareIntent = new Intent(); 
    // shareIntent.setAction(Intent.ACTION_SEND); 
    // shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 
    // shareIntent.setType("text/plain"); 
    // startActivity(shareIntent); 
    // return shareIntent; 
    //} 
} 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.yoalfaaz.yoalfaaz.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <android.support.v4.widget.SwipeRefreshLayout 

     android:id="@+id/swiperefresh" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <WebView 
      android:id="@+id/webview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 

    </android.support.v4.widget.SwipeRefreshLayout> 
</android.support.constraint.ConstraintLayout> 

這些負責刷卡刷新兩個文件,你可以看到所有在那裏的代碼。我希望你能夠弄清楚這裏的問題到底是什麼。

+0

是因爲崩潰?它到baseAvitivity –

+0

@AnkitAman該應用程序工作正常,但是,也許刷卡後,它會去baseActivity。你能告訴如何解決這個問題嗎? –

+0

你可以在刷卡後放一些日誌嗎? –

回答

1

您的SwipeRefreshLayout.OnRefreshListener正在調用LoadWeb方法。在LoadWeb內部,您打電話給YoWeb.loadUrl(「https://www.yoalfaaz.com」);這意味着您始終將webview設置爲在刷新時轉到該URL。改變你的代碼是這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    YoWeb = (WebView)findViewById(R.id.webview); // Move your declaration up here 
    swipe = (SwipeRefreshLayout) findViewById(R.id.swiperefresh); 
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      LoadWeb(YoWeb.getUrl()); // Pass in the current url to refresh 
     } 
    }); 

    LoadWeb("https://www.yoalfaaz.com"); // load the home page only once 
} 

public void LoadWeb(String url) // Pass in URL you want to load 
{ 

    WebSettings webSettings = YoWeb.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    YoWeb.loadUrl(url); // Load the URL passed into the method 
    swipe.setRefreshing(true); 
    YoWeb.setWebViewClient(new WebViewClient() { 

     //onPageFinished Method 
     public void onPageFinished(WebView view, String url) { 
      //Hide the SwipeRefreshLayout 
      swipe.setRefreshing(false); 
     } 
    }); 
} 
+0

非常感謝,你的代碼像魅力一樣工作。你能說出你做了什麼改變,所以我可以更好地理解它嗎? –

+0

在我所做的每一項變更旁邊都有評論。 –