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")을 호출합니다. 즉 새로 고침을 할 때 항상 해당 URL로 이동하도록 webview를 설정합니다. 코드를 다음과 같이 변경하십시오.

@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

나는 모든 변경 사항 옆에 의견이 있습니다. –

관련 문제