0

I make navigation in my app via Navigation Architecture Component https://developer.android.com/topic/libraries/architecture/navigation/

I have one MainActivity and switching fragments inside it.

public class MainActivity extends AppCompatActivity {
  NavController mNavController;
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    ..... 
    Navigation.findNavController(this, R.id.my_nav_host_fragment);
    .....
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.map: {
            mNavController.navigate(R.id.action_global_mapFragment);
            break;
        }
    }
    return super.onOptionsItemSelected(item);
  }
}

and in MapFragment I have something like this (load html with iframes):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  ......
  mWvMap.setWebChromeClient(new WebChromeClient());
  ....
  mWvMap.loadData("<html>\n" +
                "<head>\n" +
                "</head>\n" +
                "<body style=\"margin: 0\">\n" +
                ......

}

My problem: each time, when I click map icon in toolbar, mNavController.navigate(R.id.action_global_mapFragment); called and each time new MapFragment created, and each time it's load heavyweight html data.

How I can cache it? I can't store webview state in onPause, because fragment recreated each time. Can I setup NavigationController to avoid this?

Or any other solution possible?

igor_rb
  • 1,821
  • 1
  • 19
  • 34

1 Answers1

1

Navigate method always create a new instance of fragment, you can try to use web caching:

mWvMap.setAppCacheEnabled(true); 
mWvMap.setAppCachePath(getBaseContext().getCacheDir().getPath());
Alex
  • 9,102
  • 3
  • 31
  • 35