我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
来自活动
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");
}
其他回答
换句话说,您可以使用接口传递数据。
我们有两个活动A,B,那么我该怎么做,创建一个界面,如:
public interface M{
void data(String m);
}
然后,您可以调用赋值给这个方法,如下面A类中的代码所示:
public class A extends AppCompatActivity{
M m; //inteface name
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
m= (M) getActivity();
//now call method in interface and send data im sending direct you can use same on click
m.data("Rajeev");
}
}
现在您必须在类B中实现该接口:
public class B extends AppCompatActivity implements M{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
@Override
public void data(String m) {
you can use m as your data to toast the value here it will be same value what you sent from class A
}
}
另一种方法是使用存储数据的公共静态字段,即:
public class MyActivity extends Activity {
public static String SharedString;
public static SomeObject SharedObject;
//...
最简单的方法是将会话id传递给用于启动活动的Intent中的注销活动:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
访问下一个活动的意图:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
Intents的文档有更多信息(请参阅标题为“附加”的部分)。
如果要在活动/片段之间传输位图
活动
在活动之间传递位图
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);
试试看:
当前活动.java
Intent intent = new Intent(currentActivity.this, TargetActivity.class);
intent.putExtra("booktype", "favourate");
startActivity(intent);
目标活动.java
Bundle b = getIntent().getExtras();
String typesofbook = b.getString("booktype");