0

我有一个问题,那就是......我有一个用户登录的 LoginActivity 和一个 DashboardActivity,如果用户成功登录,他将被重定向到仪表板。

当我登录我得到的 json 对象时,如下所示:

{
  "id": 9,
  "email": "mario@gmail.com",
  "password": "123456"
}

最后。我的问题是,有没有办法访问 DashboardActivity 中的用户 ID?

我使用的堆栈如下:Android Studio、Retrofit、GSON 和 Node.js。

祝你今天过得愉快!

4

1 回答 1

1

您可以通过以下方式访问仪表板活动中的用户ID。

  • 使用捆绑包:在启动仪表板活动之前,您可以在意图中设置值,如下所示:

    Intent i = new Intent(LoginActivity.this, DashboardActivity.class);
    String id= valueofId;// 这里设置id的值。i.putExtra("STRING_I_NEED", id);

您可以检索该值,如下所示: String userId;

Bundle extras = getIntent().getExtras();
if(extras != null) {
    userId = extras.getString("STRING_I_NEED");
}
  • 使用共享首选项:您可以在 loginActivity 中保存数据并在 Dashboardactivity 中检索

如何保存数据:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score_key), newHighScore);
editor.commit();

如何获取数据:

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

就是这样。快乐编码!谢谢!

于 2020-06-05T11:42:09.110 回答