gpt4 book ai didi

android - Activity 创建了两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:29 28 4
gpt4 key购买 nike

我已经设置了锁定方向

enter image description here

并添加了带有 2 个简单类的示例代码,如下所示:

SplashLandscapeActivity.java

public class SplashLandscapeActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("start", "xxxx start Activity SplashLandscapeActivity");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashLandscapeActivity.this, TestActivity.class));
finish();
}
}, 500);
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d("start", "xxxx onDestroy Activity SplashLandscapeActivity");
}
}

TestActivity.java

public class TestActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("start", "xxxx start Activity TestActivity "
+ getResources().getConfiguration().orientation);
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d("start", "xxxx onDestroy Activity TestActivity "
+ getResources().getConfiguration().orientation);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashLandscapeActivity"
android:theme="@style/SplashTheme"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name=".TestActivity"
android:screenOrientation="portrait"/>
</application>
</manifest>

当我使用new Handler().postDelayed (SplashLandscapeActivity.java) 启动TestActivity时,启动了两次,第一次有Landscape 方向然后切换回portrait。日志显示了一切:

xxxx start Activity SplashLandscapeActivity

xxxx start Activity TestActivity 2 // <== landscape

xxxx onDestroy Activity TestActivity 1

xxxx start Activity TestActivity 1 // <== portrait

xxxx onDestroy Activity SplashLandscapeActivity

如果我删除那个 HandlerTestActivity 现在会像往常一样以纵向启动。

xxxx start Activity SplashLandscapeActivity

xxxx start Activity TestActivity 1

xxxx onDestroy Activity SplashLandscapeActivity

所以,我的问题是:

1- 此系统问题或其预期行为是什么?为什么即使在 Manifest 中设置了固定的 screenOrientation 也会重新启动 activity

2- 实际上,我的真实项目没有任何 Handler 但有同样的问题 activity 启动了两次(在 Intent.FLAG_ACTIVITY_NEW_TASK | Intent 启动之后.FLAG_ACTIVITY_CLEAR_TASK)。我该如何处理这个问题?

最佳答案

在您的 list 文件中,像这样编辑 TestActivity block

<activity
android:name=".TestActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>

关于android - Activity 创建了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44104109/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com