Android - 在同一个 class 中创建新 Intent

Android - Create new Intent within the same class

我想知道是否有人对此有解决方案。我在 Activity A 中,我想为 Activity A 打开一个新实例,所以我执行以下操作:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

这是清单文件

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:name=".data.App"
        android:theme="@style/Theme.Sherlock.Light"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.Sherlock.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=“.controllers.A”
            android:theme="@style/Theme.Sherlock.Light"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"></activity>
       </application>

</manifest>

您的代码是正确的

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

检查你的 AndroidManifest.xml ,可能你的 activity 定义了 launchMode ,比如 android:launchMode="singleTask"

更新:你的问题是你正在使用android:launchMode="singleTask",删除这个属性。

我想通了。我必须有 launchMode = 'standard',我也有一些错误的逻辑,这就是为什么它看起来好像没有实际工作的原因。