Android应用程序可以启动没有意图过滤器的活动吗? [英] Can Android Application Launch Start an Activity That Has No Intent-Filter?

查看:103
本文介绍了Android应用程序可以启动没有意图过滤器的活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序中有许多内部"活动,我只希望通过编写的代码从应用程序内部启动.这些内部"活动在Android清单文件中没有intent-filter标签.我有一个名为SplashActivity的活动,用作具有典型启动意图过滤器的初始屏幕:

I have many "internal" activities in my Android application that I only want to be started from inside my application by code I've written. These "internal" activities have no intent-filter tag in the Android manifest file. I have one activity, named SplashActivity, that I use as a splash screen that has the typical launch intent-filter:

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

我希望/期望每当Android启动我的应用程序并创建我的自定义Application对象时,它将始终启动我的SplashActivity.但是,我的一些用户遇到了启动,其中启动了我的没有意图过滤器的内部"活动之一.我认为活动通常是该应用程序先前调用中使用的最后一个活动.我自己无法重现该问题.但是,在某些情况下,Android将启动我的应用程序,创建我的自定义Application对象,但是启动我的没有意图过滤器的内部活动之一. Android在什么情况下会这样做?

I was hoping/expecting that whenever Android launched my app and created my custom Application object, it would always start my SplashActivity. However, some of my users have encountered launches where one of my "internal" activities which have no intent-filter are started. I believe that activity was typically the last activity used in a previous invocation of the app. I have not been able to reproduce the issue myself. However, is there some scenario where Android will launch my app, creating my custom Application object, but starting one of my internal activities that has no intent-filter. Under what circumstances will Android do so?

要轻松重现Android应用程序启动启动没有意图过滤器的活动的情况,请首先对任何此类活动打开应用程序.按下主屏幕按钮.然后使用Android设备监视器(DDMS)查找正在运行您的应用程序的进程并停止/终止该进程.然后启动您的应用程序. Android将创建您的Application对象,但将启动/恢复上次显示的活动,而不是使用MAIN LAUNCHER intent-filter启动该活动.

To easily reproduce the scenario where Android application launch starts an activity that has no intent-filter, first open your application to any such activity. Press the Home button. Then using Android Device Monitor (DDMS) to find the process that is running your application and stop/kill that process. Then launch your app. Android will create your Application object but will start/restore the activity that was last displayed instead of the starting the one with the MAIN LAUNCHER intent-filter.

推荐答案

Android可以随时终止托管您的应用的OS进程.通常,当您的应用在后台运行了一段时间(即:用户离开您的应用进行其他操作)时,就会发生这种情况.这种情况一直都在发生.

Android can kill the OS process hosting your app at any time. Usually this happens when your app has been in the background for a while (ie: the user navigated away from your app to go do something else). This happens all the time.

当用户随后返回到您的应用程序时,Android为该应用程序创建一个新的OS进程,并创建堆栈顶部的Activity的新实例(即:正在显示的Activity在将应用程序推送到后台之前在屏幕上显示).

When the user then returns to your app, Android creates a new OS process for the app, and creates a new instance of the Activity that was on the top of the stack (ie: the Activity that was showing on screen before the app was pushed to the background).

如果您不希望发生这种情况,可以将以下属性添加到SplashActivity<activity>声明中:

If you don't want this to happen, you can add the following attribute to the <activity> declaration for SplashActivity:

android:clearTaskOnLaunch="true"

如果用户返回,这将强制Android始终从头开始重新启动您的应用.但是,这可能会使您的用户抱怨,因为如果用户使用您的应用程序,然后打了一个电话,然后又返回到您的应用程序,则它将从头开始.

This will force Android to always restart your app from the beginning if your user returns to it. However, this might make your users complain, because if the user is using your app, then takes a phone call, then returns to your app, it will start from the beginning again.

最好自己检测出问题,然后在必要时(例如:由于进程已被终止并重新启动而需要初始化您的应用程序时),仅重定向到SplashActivity .为此,请在SplashActivity成功初始化应用后,在SplashActivity中声明一个名为initializedstatic变量,并将其设置为true.在其他每个Activity中,在onCreate()中执行此操作:

It is better if you detect the problem yourself, and redirect to the SplashActivity only when necessary (ie: when your app needs to be initialized because the process has been killed and restarted). To do this, declare a static variable named initialized in SplashActivity that you set to true when your SplashActivity has successfully initialized the app. In every other Activity, do this in onCreate():

super.onCreate(savedInstanceState);
if (!SplashActivity.initialized) {
    // Android killed my process, need to redirect the user to `SplashActivity`
    redirectIntent = new Intent(this, SplashActivity.class);
    startActivity(redirectIntent);
    finish();
    return;
}

这篇关于Android应用程序可以启动没有意图过滤器的活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆