我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
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类使用序列化在类之间传递数据。
其他回答
在活动之间传递数据最方便的方法是传递意图。在要发送数据的第一个活动中,应添加代码,
String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
v.getContext().startActivity(intent);
您还应导入
import android.content.Intent;
然后在下一个活动(SecondActivity)中,您应该使用以下代码从意图中检索数据。
String name = this.getIntent().getStringExtra("name");
试试看:
当前活动.java
Intent intent = new Intent(currentActivity.this, TargetActivity.class);
intent.putExtra("booktype", "favourate");
startActivity(intent);
目标活动.java
Bundle b = getIntent().getExtras();
String typesofbook = b.getString("booktype");
尝试执行以下操作:
创建一个简单的“助手”类(意图工厂),如下所示:
import android.content.Intent;
public class IntentHelper {
public static final Intent createYourSpecialIntent(Intent src) {
return new Intent("YourSpecialIntent").addCategory("YourSpecialCategory").putExtras(src);
}
}
这将是你所有意图的工厂。每次需要一个新的Intent时,在IntentHelper中创建一个静态工厂方法。要创建新的意向,您应该这样说:
IntentHelper.createYourSpecialIntent(getIntent());
在您的活动中。当您想在“会话”中“保存”某些数据时,只需使用以下方法:
IntentHelper.createYourSpecialIntent(getIntent()).putExtra("YOUR_FIELD_NAME", fieldValueToSave);
并发送此意向。在目标“活动”中,您的字段将显示为:
getIntent().getStringExtra("YOUR_FIELD_NAME");
所以现在我们可以像旧会话一样使用Intent(如servlet或JSP)。
在android应用程序的活动和其他组件之间传递数据的方法不止一种。一种是使用意图和可分割性,正如很多答案中已经提到的。
另一种优雅的方式是使用Eventbus库。
从排放活动:
EventBus.getDefault().postSticky("--your Object--");
在录制活动中:
EventBus.getDefault().removeStickyEvent("--Object class--")
其他需要考虑的事项:
提供了更多的自由,您可以传递复杂的对象,而无需以任何形式修改它们。不仅限于在活动之间传递数据,一旦设置了库,就可以使用它在应用程序管道中将数据从一个位置传递到另一个位置。例如,将此用于BottomSheetMenu到活动通信。稳定的库。简化了组件之间的通信解耦事件发送器和接收器在UI工件(例如活动、碎片)和后台线程中表现良好避免复杂和容易出错的依赖关系和生命周期问题速度快;专为高性能而优化很小(约60k罐)通过安装量超过1000000000的应用程序在实践中得到了验证具有高级功能,如传递线程、订户优先级等。
在当前活动中,创建一个新的意向:
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。