onPause和onResume方法在恢复用户上次状态时出错

时间:2017-10-16 13:38:20

标签: java android android-webview

在webview app关闭暂停之前,要恢复用户的最后一页。但是我在onPauseonResume方法中遇到错误,我不知道如何解决错误cannot resolve symbol context | cannot resolve method put

@Override
protected void onPause() {
// cannot resolve symbol context
// cannot resolve method put
    super.onPause();
    SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    SharedPreferences.Editor edit = prefs.edit();
    edit.put("lastUrl", exfilevbrowser.getUrl());
    edit.commit();
}

@Override
protected void onResume() {
    super.onResume();
 // cannot resolve symbol context
    if(exfilevbrowser != null) {
        SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
        String s = prefs.getString("lastUrl","");
        if(!s.equals("")) {
            appLoadUrl(s, false);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在某个活动中,请直接使用getSharedPreferences()代替context.getApplicationContext().getSharedPreferences()

如果您在片段内,请使用getContext().getSharedPreferences()

然后使用edit.putString()代替edit.put()