作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个每个用户唯一的用户名,我想发送给某个 Activity ,但我不想使用 Intent :
//create an intent and sends username
Intent intent = new Intent(RegisterOwner.this, Owner.class);
intent.putExtra("USERNAME",usernam);
startActivity(intent);
我想向 Activity 发送数据而不去那个 Activity ,startActivity(intent);
让我去 Activity ;我不想这样。
我只想发送数据而不启动其他 Activity 。
最佳答案
我想您希望在 Activity 之间提供特定的数据。实现此目的的方法之一是使用 SharedPreferences。
来自您的第一个 Activity (RegisterOwner)
SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("USERNAME",usernam);
editor.apply();
执行此操作后,用户名将存储在共享首选项中。现在您可以在整个应用程序中使用这些数据。
因此,您可以从 Owner Activity 中按如下方式检索:
SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME", Context.MODE_PRIVATE);
String username = mySharedPreferences.getString("USERNAME", "");
关于android - 有没有办法在不使用 Intent 的情况下在 Activity 之间发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51794094/
我是一名优秀的程序员,十分优秀!