我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
要访问所有活动中的会话id,必须将会话id存储在SharedPreference中。
请参见下面我用于管理会话的类,您也可以使用该类。
import android.content.Context;
import android.content.SharedPreferences;
public class SessionManager {
public static String KEY_SESSIONID = "session_id";
public static String PREF_NAME = "AppPref";
SharedPreferences sharedPreference;
SharedPreferences.Editor editor;
Context mContext;
public SessionManager(Context context) {
this.mContext = context;
sharedPreference = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
editor = sharedPreference.edit();
}
public String getSessionId() {
return sharedPreference.getString(KEY_SESSIONID, "");
}
public void setSessionID(String id) {
editor.putString(KEY_SESSIONID, id);
editor.commit();
editor.apply();
}
}
//Now you can access your session using below methods in every activities.
SessionManager sm = new SessionManager(MainActivity.this);
sm.getSessionId();
//below line us used to set session id on after success response on login page.
sm.setSessionID("test");
其他回答
您还可以通过创建可分割类来传递自定义类对象。使其可分割的最佳方法是编写类,然后简单地将其粘贴到如下站点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对象。
使用捆绑包@链接https://medium.com/@nikhildhyani365/从一个活动到另一个活动的传递数据-18df2a701142//从介质复制
Intent I = new Intent(MainActivity.this,Show_Details.class);
Bundle b = new Bundle();
int x = Integer.parseInt(age.getText().toString());
int y = Integer.parseInt(className.getText().toString());
b.putString("Name",name.getText().toString());
b.putInt("Age",x);
b.putInt("ClassName",y);
I.putExtra("student",b);
startActivity(I);
使用Intent@链接https://android.jlelse.eu/passing-data-between-activities-using-intent-in-android-85cb097f3016
在当前活动中,创建一个新的意向:
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。
Destination活动的定义如下:
public class DestinationActivity extends AppCompatActivity{
public static Model model;
public static void open(final Context ctx, Model model){
DestinationActivity.model = model;
ctx.startActivity(new Intent(ctx, DestinationActivity.class))
}
public void onCreate(/*Parameters*/){
//Use model here
model.getSomething();
}
}
在第一个活动中,按如下方式开始第二个活动:
DestinationActivity.open(this,model);
如果使用kotlin:
在MainActivity1中:
var intent=Intent(this,MainActivity2::class.java)
intent.putExtra("EXTRA_SESSION_ID",sessionId)
startActivity(intent)
在MainActivity2中:
if (intent.hasExtra("EXTRA_SESSION_ID")){
var name:String=intent.extras.getString("sessionId")
}