【问题标题】:onNewIntent not being firedonNewIntent 没有被解雇
【发布时间】:2018-05-14 13:42:18
【问题描述】:

我正在使用意图过滤器为我的应用捕获自定义方案:

<activity
    android:name=".activities.MyActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/title_my_activity"
    android:launchMode="singleTop"
    android:noHistory="true"
    android:screenOrientation="portrait">

    <!-- myapp://app.open -->
    <intent-filter android:label="@string/my_intent">
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="myapp" />
        <data android:host="app" />
        <data android:host="app.open" />
    </intent-filter>
</activity>

在我的活动中我覆盖:

@Override
protected void onNewIntent(Intent intent) {
    Log.d("DEBUG", "New intent!");
    super.onNewIntent(intent);
}

但这从来没有被调用过……我哪里错了? noHistory 标志是必要的吗?如果我删除它,我在堆栈上有两个相同活动的副本。

【问题讨论】:

  • android:launchMode="singleTask"怎么样?
  • 似乎有效,除了 onResume 被调用了两次......您能否在答案中详细说明原因?非常感谢。
  • 你的应用中还有其他活动吗?

标签: android android-intent android-activity android-lifecycle url-scheme


【解决方案1】:

以下代码对我来说很好用

<activity android:name=".MainActivity"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="myapp" />
            <data android:host="app" />
            <data android:host="app.open" />
        </intent-filter>
    </activity>


 public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this,MainActivity.class);
    startActivity(intent);

}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d("DEBUG", "New intent!");
}}

【讨论】:

    猜你喜欢
    • 2020-03-28
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 2016-07-15
    • 2021-04-05
    相关资源
    最近更新 更多