android - 用于创建 Activity 的 Intent 是否存在?

标签 android android-intent android-activity oncreate

关于这个问题的更多内容。

我想知道如果我不杀死它,我在创建 Activity 的 Intent 中传递的数据是否会保留。

例子: Activity A 使用额外的数据 String(SomeStringValue) 调用 Activity B。 然后 Activity B 调用 C 调用 D。现在在这段时间的某个地方 Activity B 被销毁(例如为了节省内存),当我回到 Activity B 时需要重新创建它(例如再次调用 onCreate)但是因为我已经使用后退按钮而不是传递 Intent ,我以前的 Intent 是否仍然存在并且我可以获得我需要的数据或者这些数据是否会消失。

我已经尝试自己测试它但是我无法在不终止整个应用程序的情况下再次调用 onCreate。

最佳答案

如上所述,我通过进入开发人员选项并打开“不保留 Activity ”来对此进行测试。

使用这种方法,我发现当 Activity 从内存中删除时,原始 Intent 得以保留。

当我离开一个 Activity 时,onDestroy 会立即被调用。当我返回到原始 Activity 时,调用 onCreate 时 Intent 中的值与最初发送的值相同。

以下代码用作我的测试平台。

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String extra = getIntent().getStringExtra("test");

    ((TextView) findViewById(R.id.test)).setText(extra);
}

public void onClick(View view) {
    Intent i = new Intent(this, MyActivity.class);
    i.putExtra("test", ""+Math.random());
    startActivity(i);
}

@Override
protected void onDestroy() {
    Log.d("Test", "onDestroy");
    super.onDestroy();    //To change body of overridden methods use File | Settings | File Templates.
}

因此,为了回答您的问题,在 onSavedInstanceState 中保存 Intent 数据是多余的。您应该只保存任何已更改或需要保留但不会永远保留的内容。

关于android - 用于创建 Activity 的 Intent 是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124675/

相关文章:

java - 安卓转接电话

Android 启动模式不起作用

android - 处理 Android Volley 请求序列的良好做法

java - 使用 Android 模式实现电子邮件 validator

android - 我在 intelliJ 中的创建新项目下看不到 Android 类型的选项

android - 无法使用 FileProvider 在 Android N 中打开外部存储文件

安卓 :Capture media button action when Music player is Active

android - 如何在 android 通话结束后启动 Activity ?

android - 在不关闭 Activity 的情况下切换 Activity

java - Android工具栏后退按钮错误