【问题标题】:Can we share or transfer data from one Activity to another Activity using SharedPreference?我们可以使用 SharedPreference 将数据从一个 Activity 共享或传输到另一个 Activity 吗?
【发布时间】:2017-07-12 11:45:32
【问题描述】:

我只需将保存文件路径从一个活动传输到另一个活动,但在显示数据时它显示空值。

以下是在 SharedPreference 中存储数据的代码:-

    SharedPreferences pref= getApplicationContext().getSharedPreferences("Recorder",0);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("file_name",DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+".mp4");
    editor.putString("file_path",Environment.getExternalStorageDirectory()+"/"+DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+".mp4");

从 sharedPreference 中检索数据:-

 file_name = (TextView) findViewById(R.id.file_name);
    file_path = (TextView) findViewById(R.id.file_path);
    pref = getApplicationContext().getSharedPreferences("Recorder",0);
    editor = pref.edit();
    String fileName = pref.getString("file_name",null);
    String filePath = pref.getString("file_path",null);

    if(fileName != null){
            file_name.setText(fileName);
    }else{
        file_name.append("");
    }

    if (filePath != null){
        file_path.setText(filePath);
    }else{
        file_path.append("");
    }

【问题讨论】:

  • 添加 editor.apply();在将值置于偏好之后
  • 是的,您可以,editor.apply() 是您所缺少的;尽管更好的方法是使用intent.putStringExtra()
  • 你也可以写editor.commit(); stroing the values to sharepreference

标签: java android android-activity sharedpreferences


【解决方案1】:

你必须通过

应用编辑器

editor.commit():

editor.apply();

【讨论】:

    【解决方案2】:

    editor.apply() 丢失。您没有提交保存。

    【讨论】:

      【解决方案3】:

      为什么需要超载系统?您有一种在活动之间传递数据的机制,只需在意图中发送文件的路径。

      在 ActivityA 中:

      Intent intent = new Intent(ActivityA.this, ActivityB.class);
      intent.putExtra("filePath", "...");
      startActivity(intent);
      

      在 ActivityB 中:

      String filePath = getIntent().getExtras().getString("filePath");
      

      【讨论】:

      • 实际上这个问题要求使用偏好而不是额外的意图来传输数据
      • 是的,但是如果您想以正确的方式进行操作,那绝对是推荐的...
      【解决方案4】:

      是的,您可以使用 SharedPreference 来存储信息。

      要将数据从一个活动传递到另一个活动,最好使用意图。

          Intent intent = new Intent(CurrentActivity.this, AnotherActivity.class);
          intent.putExtra("file_name","name");
          intent.putExtra("file_path","path");
          startActivity(intent)
      

      在另一个活动中,

      String fileName = getIntent().getStringExtra("file_name");
      String filePath = getIntent().getStringExtra("file_path");
      

      【讨论】:

        【解决方案5】:

        如果您使用 SharedPreffernces 只是为了在 Activity 之间共享数据,那就有点过头了。

        如果应用重新启动后数据应该可用,则您的解决方案是好的。但如果您只在运行时需要它,最好使用其他答案中提到的 Intent。

        就个人而言,我更喜欢将其存储在某个静态变量中。

        【讨论】:

          【解决方案6】:

          editor.commit() 丢失。 更好的方法是使用可以在 Intent 中设置的包,并使用此 Intent 启动另一个活动,然后从 Intent 的包中获取值。

          【讨论】:

            【解决方案7】:

            是的,您可以使用 SharedPrefernces。试试下面的代码

                 public static final String MYPREFERNCES_Example = "mysharedpref";
                 SharedPreferences.Editor editor ;
                 SharedPreferences sp;
            
                sp = getSharedPreferences(MYPREFERNCES_Example Context.MODE_PRIVATE);
                editor=sp.edit();
            
                Intent i_login = new Intent(LoginActivity.this, HomeActivity.class);
                editor.putString("yourownkey1", yourvalue);
                editor.putString("yourownkey2", yourvalue);
                editor.putString("yourownkey3", yourvalue);
                startActivity(intent)
            

            在下一个活动中

            sp = getSharedPreferences(MYPREFERNCES_Example, 0);   
            editor = sp.edit();  
            String fn = sp.getString("yourownkey1", "");
            String ln = sp.getString("yourownkey2", "");
            String fbemail = sp.getString("yourownkey3", "");
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-04-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-12-28
              相关资源
              最近更新 更多