从搜索中启动 Android Activity

标签 android

我的 Activity :

A - 主 Activity (一种登录屏幕),finish() 去到任何其他 Activity

B - 用户内容

C = 其他用户内容

当我转到 A > B > C 时,按主页,从启动器启动应用程序,我看到 C 的返回堆栈已恢复 B > C(顶部),这里没问题

当我转到 A > B > C 时,按主页,从主屏幕上的谷歌搜索栏启动应用程序,我看到 A,返回堆栈 B > C > A(顶部)。

问题是为什么会这样,我该如何解决?

list 中的 fragment :

<activity
  android:name="ActivityA"
  android:label="@string/app_name"
  android:launchMode="standard"
  android:windowSoftInputMode="stateHidden|adjustPan" >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity 
    android:name="ActivityB"
    android:label="@string/b_screen_title" >
  </activity>
  <activity
    android:name="ActivityC"
    android:label="@string/c_screen_title"
    android:windowSoftInputMode="stateVisible|adjustResize" >
  </activity>

最佳答案

当您使用 Google 搜索栏然后选择您的应用时,它会启动根 Activity(在您的示例中为 ActivityA)。这不同于启动程序在启动您的应用程序时所做的事情(如果应用程序已经在运行,它只是将现有任务带到前台)。要在您的应用中模拟此行为,您可以将以下代码添加到 ActivityA.onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate();
    // If we are not the root of this task, it means that this activity has been launched
    //  by another mechanism (ie: Google Search)
    if (!isTaskRoot()) {
        // This isn't the root of this task, so just go away quietly and drop the user
        //  into the application wherever he left it
        finish();
        return;
    }
    // ...the rest of your onCreate() goes here...
}

关于从搜索中启动 Android Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16530081/

相关文章:

android - Android 中的星形按钮

java - 线程中对象的速度不准确

android - 如何调整ListView中的ImageView?

android - ButterKnife错误:找不到符号方法bind(MyActivity)

android - 改造具有不同对象的数组

android - Html.fromHtml 函数的 "font"标记中有哪些字体可用?

android - 我无法在底部显示 LinearLayout 以 ScrollView

java - 如何检索 Firebase 集合中的最后一个文档。我还想获取文档字段值

java - 裁剪后如何从图库中选择图像并保存在应用程序中?

android - 如何在谷歌地图上添加多边形?