我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
使用全局类:
public class GlobalClass extends Application
{
private float vitamin_a;
public float getVitaminA() {
return vitamin_a;
}
public void setVitaminA(float vitamin_a) {
this.vitamin_a = vitamin_a;
}
}
您可以从所有其他类调用该类的setter和getter。要做到这一点,您需要在每个活动中创建一个GlobalClass对象:
GlobalClass gc = (GlobalClass) getApplication();
然后,您可以调用例如:
gc.getVitaminA()
其他回答
另一种方法是使用存储数据的公共静态字段,即:
public class MyActivity extends Activity {
public static String SharedString;
public static SomeObject SharedObject;
//...
您可以使用Intent
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);
另一种方法可以是使用单例模式:
public class DataHolder {
private static DataHolder dataHolder;
private List<Model> dataList;
public void setDataList(List<Model>dataList) {
this.dataList = dataList;
}
public List<Model> getDataList() {
return dataList;
}
public synchronized static DataHolder getInstance() {
if (dataHolder == null) {
dataHolder = new DataHolder();
}
return dataHolder;
}
}
从您的第一次活动
private List<Model> dataList = new ArrayList<>();
DataHolder.getInstance().setDataList(dataList);
关于第二项活动
private List<Model> dataList = DataHolder.getInstance().getDataList();
使用回调在活动之间进行新的实时交互:
-步骤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获取一些结果或调用一些方法
你只需要在表达你的意图时发送额外的信息。
这样地:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
现在,在SecondActivity的OnCreate方法中,您可以像这样获取额外内容。
如果您发送的值过长:
long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));
如果发送的值是字符串:
String value = getIntent().getStringExtra("Variable name which you sent as an extra");
如果发送的值是布尔值:
Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);
我使用公共静态字段来存储活动之间的共享数据,但为了最大限度地减少其副作用,您可以:
只创建一个或尽可能少的字段,然后重用它们,使它们成为对象类型,并在接收活动中将其转换为所需类型。每当它们中的任何一个不再有用时,在下一次分配之前,将其显式设置为空,由垃圾收集器收集。