您可以通过以下方式访问仪表板活动中的用户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);
就是这样。快乐编码!谢谢!