我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
我们可以通过两种方式将值传递给另一个Activity(已经发布了相同类型的答案,但我在这里发布了通过意图尝试的redcing代码)
1.通过意图
Activity1:
startActivity(new Intent(getApplicationContext(),Activity2.class).putExtra("title","values"));
InActivity 2:
String recString= getIntent().getStringExtra("title");
2.通过SharedPreference
Activity1:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
// 0 - for private mode
Editor editor = pref.edit();
editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes
Activty2:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
pref.getString("key_name", null); // getting String
其他回答
数据对象应扩展Parcelable或Serializable类。
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);
Intent intent = new Intent(getBaseContext(), SomeActivity.class);
intent.putExtra("USER_ID", UserId);
startActivity(intent);
On SomeActivity :
String userId= getIntent().getStringExtra("("USER_ID");
标准方法。
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
Bundle bundle = new Bundle();
bundle.putString(“stuff”, getrec);
i.putExtras(bundle);
startActivity(i);
现在,在第二个活动中,从捆绑包中检索数据:
获取捆绑包
Bundle bundle = getIntent().getExtras();
提取数据…
String stuff = bundle.getString(“stuff”);
Intent intent = new Intent(YourCurrentActivity.this, YourActivityName.class);
intent.putExtra("NAme","John");
intent.putExtra("Id",1);
startActivity(intent);
您可以在其他活动中检索它。两种方式:
int id = getIntent.getIntExtra("id", /* defaltvalue */ 2);
第二种方法是:
Intent i = getIntent();
String name = i.getStringExtra("name");
你可以用心工作。
String sessionId = "my session id";
startActivity(new Intent(getApplicationContext(),SignOutActivity.class).putExtra("sessionId",sessionId));