我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
您可以通过意图在两个活动之间进行沟通。每当您通过登录活动导航到任何其他活动时,都可以将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 intent = new Intent(getApplicationContext(), ClassName.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
第二项活动:
String str= getIntent().getStringExtra("Variable name which you sent as an extra");
/*
* 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));
}
}
我们可以通过两种方式将值传递给另一个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
使用回调在活动之间进行新的实时交互:
-步骤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获取一些结果或调用一些方法
在当前活动中,创建一个新的意向:
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。