如何防止在导航抽屉中刷新 webview [英] How to prevent refreshing of webview in navigation drawer

查看:26
本文介绍了如何防止在导航抽屉中刷新 webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直使用 viewpager 在活动中显示 webview 页面,但现在我已从 viewpager 更改为 带有 RecyclerView 的导航抽屉,为了显示 webview,我的活动中有多个 webview 加载.

From a while i had been using viewpager to display webview pages in activity, but now i have change from viewpager to navigation drawer with RecyclerView, to display webview, i have multiple webview loading in my activity.

假设活动从 webview 1 开始,然后我点击 webview 2 如果我回到 webview 1 它将重新加载(刷新) 而我想防止这种情况发生.

Suppose activity starts on webview 1 and then i click on webview 2 if i go back to webview 1 it will be reloading(refresh) and i want to prevent that from happening.

请帮忙.

这是我的主要活动.

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);
    }
}
}

推荐答案

你是说要恢复同一个片段?这是来自 developer.android.com

Are you saying that you want to restore the same fragment? This is from 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();

这使后退导航按钮能够恢复已经加载 webview 的相同片段.您每次都在 switch 语句中创建一个新的.

This enables the back navigation button to restore the same fragment that already loaded the webview. You are creating a new one each time in the switch statement.

一旦您让后退按钮按您想要的方式工作,您就可以轻松学习如何手动将特定片段从堆栈中拉出.

Once you get the back button working how you want it you can easily learn how to manually pull the specific fragment off of the stack.

我认为您没有刷新 web 视图.每次通过调用空构造函数选择项目时,您都在创建一个新片段.您只需要创建每个片段一次,然后在返回该 web 视图时使用该片段的相同实例.使用返回堆栈将使其在您离开应用程序时保持加载状态,并在稍后返回.

I do not think you are refreshing the webview. You are creating a new fragment each time you select an item by calling the empty constructor. You need to only create each fragment one time and then use the same instance of that fragment when you go back to that webview. Using the back stack will enable it to stay loaded while you leave the application and come back to it later.

也尝试使用静态方法 YourFragment.newInstance(params),它返回一个新的 YourFragment 实例.这样做而不是空的构造函数,因为这被认为是不正确的做法.

Also try using the static method YourFragment.newInstance(params) which returns a new instance of YourFragment. Do this instead of the empty constructor, as that is considered incorrect practice.

这篇关于如何防止在导航抽屉中刷新 webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆