13102

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

Question:

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.

Answer1:

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 <strong>only</strong> 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; }

Answer2:

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.

Answer3:

Can you try adding

android:clearTaskOnLaunch

for the root activity in your manifest file.

Recommend

  • Javascript window.opener in iframe
  • Fragment Orientation Change - Better Test than for Landscape
  • Will default route('/') add to the Navigator Stack when app loads
  • How can I bypass a “-XX:+DisableAttachMechanism” Java VM Option?
  • How to invoke a task twice in Thor
  • Understanding recursion in Java a little better
  • Microsoft.Build.Utilities.ToolLocationHelper error on TeamCity
  • Problems displaying full-screen CAEAGLLayer on Retina iPad
  • Not seeing logs from onEdit trigger
  • MySQL Query Tuning - Why is using a value from a variable so much slower than using a literal?
  • How to correctly append dynamic GetUIKit accordions?
  • C# foreach - Is collection computed with each iteration? [duplicate]
  • Dependency conflict in integrating with Cloudera Hbase 1.0.0
  • IF statement formatting best-practise, what's your style?
  • Check all commands exit code within a bash script
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • iOS - Is this a task for enums?
  • DataGridView: Pass by Value or Reference?
  • JSF2.0 + Primefaces 3.0.1 + jquery 1.6.4 + p:commandLink + IE8 throws Unexpected call to method or p
  • Spring security - same page to deliver different content based on user role
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Hash Code in SQL Server?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Moving Android View and preventing onDraw to be called over and over again
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Allowing both email and username for authentication
  • $wpdb not working in file of WordPress plugin
  • Handling un-mapped Rest path
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Fetching methods from BroadcastReceiver to update UI
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Updated Ionic CLI but shows previous version (Windows)
  • Data Validation Drop Down Box Arrow Disappearing
  • Conditional In-Line CSS for IE and Others?