Can Android Application Launch Start an Activity That Has No Intent-Filter?

纵饮孤独 提交于 2019-12-12 03:59:11

问题


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>

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?

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.


回答1:


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.

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).

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

android:clearTaskOnLaunch="true"

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.

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;
}



回答2:


This happens when the application is still "alive".

When you exit an application, Android does not kill it. It will remain there until the memory is needed. In this case, when the use re-launches it he will get the last Activity.




回答3:


Can you try adding

android:clearTaskOnLaunch

for the root activity in your manifest file.



来源:https://stackoverflow.com/questions/43463981/can-android-application-launch-start-an-activity-that-has-no-intent-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!