我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。

单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?

本案的任何替代方案


当前回答

你只需要在表达你的意图时发送额外的信息。

这样地:

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);

其他回答

Intent intent = new Intent(YourCurrentActivity.this, YourActivityName.class);
intent.putExtra("NAme","John");
intent.putExtra("Id",1);
startActivity(intent);

您可以在其他活动中检索它。两种方式:

int id = getIntent.getIntExtra("id", /* defaltvalue */ 2);

第二种方法是:

Intent i = getIntent();
String name = i.getStringExtra("name");

我最近发布了Vapor API,这是一个jQuery风格的Android框架,它使类似这样的所有任务都更加简单。如前所述,SharedPreferences是实现此目的的一种方法。

VaporSharedPreferences实现为Singleton,因此这是一个选项,在VaporAPI中,它有一个重载的.put(…)方法,因此您不必明确担心您提交的数据类型-前提是它受支持。它也很流畅,因此您可以拨打电话:

$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);

它还可以选择自动保存更改,并在后台统一读写过程,因此您不需要像在标准Android中那样显式检索编辑器。

或者,您可以使用意向。在VaporAPI中,您还可以对VaporContent使用可链接的overload.put(…)方法:

$.Intent().put("data", "myData").put("more", 568)...

如其他答案中所述,将其作为附加项传递。您可以从“活动”中检索额外内容,此外,如果您正在使用VaporActivity,这将自动为您完成,因此您可以使用:

this.extras()

要在切换到的“活动”的另一端检索它们。

希望有人对此感兴趣:)

您还可以通过创建可分割类来传递自定义类对象。使其可分割的最佳方法是编写类,然后简单地将其粘贴到如下站点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对象。

如果要在活动/片段之间传输位图


活动

在活动之间传递位图

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);

你可以用心工作。

String sessionId = "my session id";
startActivity(new Intent(getApplicationContext(),SignOutActivity.class).putExtra("sessionId",sessionId));