2016-05-29 61 views
1

從我一直在使用viewpager顯示在活動webview頁面一段時間,但現在我已經從viewpagernavigation drawer with RecyclerView變化,顯示webview,我有多個webview裝在我的活動。如何防止網頁視圖的清爽導航抽屜

假設活動開始於的WebView 1,然後我就的WebView 2單擊如果我回去webview 1將重裝(刷新),我想避免這種情況發生。

請幫忙。

這是我的主要活動。

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener { 
private Toolbar toolbar; 
private FragmentDrawer drawerFragment; 

private static String TAG = MainActivity.class.getSimpleName(); 

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

    toolbar = (Toolbar) findViewById(R.id.tool_bar); 
    if (toolbar != null) { 
     toolbar.setTitle(R.string.app_name); 
     setSupportActionBar(toolbar); 
    } 
    drawerFragment = (FragmentDrawer) 
      getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar); 
    drawerFragment.setDrawerListener(this); 


    displayView(0); 

    } 

@Override 
public void onDrawerItemSelected(View view, int position) { 
    displayView(position); 
} 

private void displayView(int position) { 
    Fragment fragment = null; 
    String title = getString(R.string.app_name); 
    switch (position) { 
     case 0: 
      fragment = new TopRatedFragment(); 
      title = getString(R.string.title_home); 
      break; 
     case 1: 
      fragment = new GamesFragment(); 
      title = getString(R.string.title_friends); 
      break; 
     case 2: 
      fragment = new MoviesFragment(); 
      title = getString(R.string.title_messages); 

     default: 
      break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container_body, fragment); 
     fragmentTransaction.commit(); 


     getSupportActionBar().setTitle(title); 
    } 
} 
} 

回答

0

你是說你想恢復相同的片段嗎?這是developer.android.com

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack so the user can navigate back 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 

這使得後退導航按鈕來恢復已加載的網頁流量相同的片段。每次在switch語句中創建一個新的。

一旦你的後退按鈕的工作你想怎麼它,你可以輕鬆地學習如何手動拉特異性片段從堆棧中。

編輯:

我不認爲你是刷新web視圖。每次通過調用空構造函數選擇一個項目時,都會創建一個新片段。您只需要一次創建每個片段,然後在返回該webview時使用該片段的同一個實例。使用後退堆棧將使其在離開應用程序時保持加載狀態,並在稍後再回來。

也可以嘗試使用靜態方法YourFragment.newInstance(params)返回YourFragment的新實例。這樣做而不是空的構造函數,因爲這被認爲是不正確的做法。

+0

我不這麼認爲,這是做這件事的正確方法。 –

+0

我已經用更多的相關信息更新了我的答案。希望這會幫助你解決你的問題。 – Chareles