【问题标题】:Android: return to the Application Home ActivityAndroid:返回Application Home Activity
【发布时间】:2012-12-29 18:23:51
【问题描述】:

我正在尝试使用下一个代码从我的应用程序堆栈返回到 My Application Home Activity:

protected void goHome(boolean offlineMode) {
    final Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
...
}

但在这种情况下,顶部的所有其他应用程序活动也将关闭。 在这种情况下是否可以只关闭我的应用程序的活动?

【问题讨论】:

    标签: android android-activity return


    【解决方案1】:

    这应该可以完成工作。

    Intent i = new Intent(context, HomeActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    finish();
    

    【讨论】:

    • 我使用旧的 sdk,没有 Intent.FLAG_ACTIVITY_CLEAR_TASK
    【解决方案2】:

    您也可以从 AndroidManifest.xml 文件中实现此功能,只需添加

    android:noHistory="true" 
    

    你想要的那些<activity>的属性(例如HomeActivity)

    这里是manifest的示例代码

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.rdc.helloWorld"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="8" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".HelloWorldActivity"
                android:label="@string/app_name" 
                android:noHistory="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    因此,当您单击主活动屏幕上的返回按钮时,它只会清除此屏幕的历史记录

    您可能喜欢阅读关于 noHistory 标签的详细信息,

    How does android:noHistory=“true” work?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多