我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
在当前活动中,创建一个新的意向:
String value="Hello world";
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("key",value);
startActivity(i);
然后在新的“活动”中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//The key argument here must match that used in the other activity
}
使用此技术将变量从一个Activity传递到另一个Activity。
其他回答
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
startActivity(intent);
然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:
if (getIntent().getExtras() != null) {
String value = getIntent().getStringExtra("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
第二种方法:您可以创建一个bundle对象并将值放入bundle中,然后将bundle对象放入当前活动的意图中-
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", value);
intent.putExtra("bundle_key", bundle);
startActivity(intent);
然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getStringExtra("bundle_key");
String value = bundle.getString("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
您还可以使用bean类使用序列化在类之间传递数据。
/*
* If you are from transferring data from one class that doesn't
* extend Activity, then you need to do something like this.
*/
public class abc {
Context context;
public abc(Context context) {
this.context = context;
}
public void something() {
context.startactivity(new Intent(context, anyone.class).putextra("key", value));
}
}
您还可以通过创建可分割类来传递自定义类对象。使其可分割的最佳方法是编写类,然后简单地将其粘贴到如下站点http://www.parcelabler.com/.单击构建,您将获得新代码。复制所有这些并替换原始的类内容。然后-
Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo", foo);
startActivity(intent);
并在NextActivity中获得结果-
Foo foo = getIntent().getExtras().getParcelable("foo");
现在,您可以像使用一样简单地使用foo对象。
您可以通过意图在两个活动之间进行沟通。每当您通过登录活动导航到任何其他活动时,都可以将sessionId设置为intent,并通过getIntent()在其他活动中获取。以下是代码段:
登录活动:
Intent intent = new Intent(YourLoginActivity.this,OtherActivity.class);
intent.putExtra("SESSION_ID",sessionId);
startActivity(intent);
finishAfterTransition();
其他活动:
在onCreate()中或需要它调用的任何位置getIntent().getStringExtra(“SESSION_ID”);此外,确保检查intent是否为空,并且在两个活动中传递的intent密钥应该相同。以下是完整的代码片段:
if(getIntent!=null && getIntent.getStringExtra("SESSION_ID")!=null){
sessionId = getIntent.getStringExtra("SESSION_ID");
}
但是,我建议您使用AppSharedPreferences存储sessionId,并在需要时从中获取。
使用Intent将数据传递到一个Activity to AnotheActivity的最佳方法,
检查剪下的代码
活动One.java
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("key_name_one", "Your Data value here");
myIntent.putExtra("key_name_two", "Your data value here");
startActivity(myIntent)
第二次活动
第二活动.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String valueOne = intent.getStringExtra("key_name_one");
String valueTwo = intent.getStringExtra("key_name_two");
}