我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
你可以用心工作。
String sessionId = "my session id";
startActivity(new Intent(getApplicationContext(),SignOutActivity.class).putExtra("sessionId",sessionId));
其他回答
在android应用程序的活动和其他组件之间传递数据的方法不止一种。一种是使用意图和可分割性,正如很多答案中已经提到的。
另一种优雅的方式是使用Eventbus库。
从排放活动:
EventBus.getDefault().postSticky("--your Object--");
在录制活动中:
EventBus.getDefault().removeStickyEvent("--Object class--")
其他需要考虑的事项:
提供了更多的自由,您可以传递复杂的对象,而无需以任何形式修改它们。不仅限于在活动之间传递数据,一旦设置了库,就可以使用它在应用程序管道中将数据从一个位置传递到另一个位置。例如,将此用于BottomSheetMenu到活动通信。稳定的库。简化了组件之间的通信解耦事件发送器和接收器在UI工件(例如活动、碎片)和后台线程中表现良好避免复杂和容易出错的依赖关系和生命周期问题速度快;专为高性能而优化很小(约60k罐)通过安装量超过1000000000的应用程序在实践中得到了验证具有高级功能,如传递线程、订户优先级等。
源类:
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("firstName", "Your First Name Here");
myIntent.putExtra("lastName", "Your Last Name Here");
startActivity(myIntent)
目标类(NewActivity类):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String fName = intent.getStringExtra("firstName");
String lName = intent.getStringExtra("lastName");
}
活动之间的数据传递主要通过意向对象进行。
首先,必须使用Bundle类将数据附加到intent对象。然后使用startActivity()或startActivityForResult()方法调用活动。
您可以从博客文章“将数据传递给活动”中找到更多信息。
使用回调在活动之间进行新的实时交互:
-步骤01:实现共享接口
public interface SharedCallback {
public String getSharedText(/*you can define arguments here*/);
}
-步骤02:实现共享类
final class SharedMethode {
private static WeakReference<Context> mContext;
private static SharedMethode sharedMethode = new SharedMethode();
private SharedMethode() {
super();
}
public static SharedMethode getInstance() {
return sharedMethode;
}
public void setContext(Context context) {
if (mContext != null)
return;
mContext = new WeakReference<Context>(context);
}
public boolean contextAssigned() {
return mContext != null && mContext.get() != null;
}
public Context getContext() {
return mContext.get();
}
public void freeContext() {
if (mContext != null) mContext.clear();
mContext = null;
}
}
-步骤03::在第一个活动中玩代码
public class FirstActivity extends Activity implements SharedCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// call playMe from here or there
playMe();
}
private void playMe() {
SharedMethode.getInstance().setContext(this);
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
@Override
public String getSharedText(/*passed arguments*/) {
return "your result";
}
}
-步骤04::在SecondActivity中完成游戏
public class SecondActivity extends Activity {
private SharedCallback sharedCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
if (SharedMethode.getInstance().contextAssigned()) {
if (SharedMethode.getInstance().getContext() instanceof SharedCallback)
sharedCallback = (SharedCallback) SharedMethode.getInstance().getContext();
// to prevent memory leak
SharedMethode.freeContext();
}
// You can now call your implemented methodes from anywhere at any time
if (sharedCallback != null)
Log.d("TAG", "Callback result = " + sharedCallback.getSharedText());
}
@Override
protected void onDestroy() {
sharedCallback = null;
super.onDestroy();
}
}
步骤05::您还可以实现backword回调(从First到Second),以从SecondAvctivity获取一些结果或调用一些方法
来自活动
int n= 10;
Intent in = new Intent(From_Activity.this,To_Activity.class);
Bundle b1 = new Bundle();
b1.putInt("integerNumber",n);
in.putExtras(b1);
startActivity(in);
目标活动
Bundle b2 = getIntent().getExtras();
int m = 0;
if(b2 != null){
m = b2.getInt("integerNumber");
}