我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话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()
其他回答
在当前活动中创建新的意向。
String myData="Your string/data here";
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("your_key",myData);
startActivity(intent);
在SecondActivity.java onCreate()中使用键your_key检索这些值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String myData = extras.getString("your_key");
}
}
对于在所有活动中使用会话id,可以执行以下步骤。
1-在应用程序的APPLICATION文件中定义一个静态变量会话(将保存会话id的值)。
2—现在用类引用调用会话变量,在那里获取会话id值,并将其分配给静态变量。
3-现在,您可以通过调用
如果要在活动/片段之间传输位图
活动
在活动之间传递位图
Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);
在“活动”类中
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
碎片
在片段之间传递位图
SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);
在SecondFragment内部接收
Bitmap bitmap = getArguments().getParcelable("bitmap");
传输大型位图
如果您正在获取失败的绑定器事务,这意味着您正在通过将大型元素从一个活动转移到另一个活动来超出绑定器事务缓冲区。
因此,在这种情况下,您必须将位图压缩为字节数组,然后在另一个活动中解压缩,如下所示
在第一个活动中
Intent intent = new Intent(this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
在第二次活动中
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
来自活动
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");
}
要访问所有活动中的会话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");