java - 为什么Android中不保留Intent

标签 java android android-intent

我正在测试我的手机可以并行创建多少个 Intent ,但只创建了一个...

这是我的应用 list

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:launchMode="standard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".IntentExamples"
        android:exported="false"
        android:launchMode="standard"
        android:parentActivityName=".MainActivity">
    </activity>
</application>

这是我进行所有调用的类...

public class IntentExamples extends AppCompatActivity {

// Numbers of intents created
static int COUNTER = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent_examples);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // When a intent is created +1 to the counter
    COUNTER++;
    ((TextView) findViewById(R.id.intent_counter)).setText("NUMBER OF INTENTS: " + COUNTER);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.another_intent:
            Intent intent = new Intent(IntentExamples.this, IntentExamples.class);
            startActivity(intent);
        case android.R.id.home:
            this.finish(); break;
        default:
            return false;
    }
    return true;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // When a intent is completely destroy, -1 to the counter
    COUNTER--;
}

主要 Activity 只是从菜单代码中复制粘贴代码...

当我点击创建另一个 Intent 的项目菜单时,在 TextView 中显示创建的 1 个 Intent ,我再次点击它,显示创建的 2 个 Intent ,当我第三次点击按钮时,它卡在创建的 2 个 Intent 中......我不明白计数器没有达到创建的 3 个或更多 Intent 的原因。

最佳答案

看看 onOptionsItemSelected() 中的这段代码

switch(item.getItemId()){
    case R.id.another_intent:
        Intent intent = new Intent(IntentExamples.this, IntentExamples.class);
        startActivity(intent);
    case android.R.id.home:
        this.finish(); break;
    default:
        return false;
}

startActivity() 之后没有 break;,因此 Activity 完成。因此最终(不一定是立即!)onDestroy() 被调用,并且 COUNTER-- 被执行。

因为 Activity 已调用 finish(),因此无法通过按 BACK 再次到达它 - 而这种“不当行为”是我无法解释的首先。我准备相信运行时最多会保持两个 Activity 实例处于 Activity 状态,并将所有其他实例存储在 BackStack 中。但回不去了?对此必须有一个解释 - 我很欣慰地找到了它:)

现在为什么旋转设备后 COUNTER 显示 1 而不是 2?

这是因为方向更改(如所有 configuration changes )会导致所有 Activity 实例被销毁,并且显示的实例会立即重新创建。

关于java - 为什么Android中不保留Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45718669/

相关文章:

java - 测试数据时如何避免空指针异常

java - 如何给两个不同的类同一个接口(interface)?

android - 非法状态异常 : Fragment YoutubeLessonFragment not attached to a context

java - 在 Play 服务 9.4.0 中访问 AdvertisingIdClient

java - ZXing IntentResult 无法解析为类型

android - ActivityResultLauncher 传递自定义请求代码

Java - 让 ArrayList 值在游戏中旋转

java - Spring调度器多次执行任务

java - android中用户定义的对象数组

android - 我想在图像选择器 Intent 中添加 "remove"选项