如何在底部导航栏片段中保存Webview状态

时间:2020-03-08 06:02:38

标签: java android webview navigation-drawer

我正在尝试在底部导航栏的3个片段中设置3个Web视图。但是,当我将片段移动到片段时,Webview再次恢复正常。如何解决这个问题。我不想重新加载。我想保留它们。

这是主要活动。java

public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    Fragment fragment = null;
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                fragment = new homeFragment();
                break;
            case R.id.navigation_dashboard:
                fragment = new BookmarkFragment();
                break;
            case R.id.navigation_notifications:
                fragment = new notificationragment();
                break;
        }
        return loadFragment(fragment);
    }
};

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

    //loading the default fragment
    loadFragment(new homeFragment());
    BottomNavigationView navView = findViewById(R.id.navigation);
    mTextMessage = findViewById(R.id.message);
    navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private boolean loadFragment(Fragment fragment) {
    //switching fragment
    if (fragment != null) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
        return true;
    }
    return false;
}

}

这是一个片段

public class homeFragment extends Fragment {

WebView webView;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, null);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    webView = getView().findViewById(R.id.web);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl("https://www.journaldev.com");     
}
}

请帮助我。

如何保留每个网络视图的状态。

0 个答案:

没有答案