Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to always start from a startup activity on Android?

Tags:

android

There are three different cases:

1) A user launches an app, navigates in it, pressed home and click on the app icon again to launch our app again.

2) A user launches an app, navigates in it, presses home, chooses recent and click on the app to launch our app again.

3) A user launches an app, navigates in it, click something in the app (TextView with a link), which calls another app (as example Email) and user clicks back button, which bring us back to our app.

I know about flag "clearTaskOnLaunch" flag, it solves case #1.

I know about about flag "excludeFromRecents", it solves case #2 (may be not the most user friendly solution, but it works).

What about case #3? I have a workaround right now. However, I will have to put it on all activities which can be lead to another app. I wonder, whether there is better way to solve it (without handling it in all such activities).

like image 651
Victor Ronin Avatar asked May 04 '12 14:05

Victor Ronin


People also ask

How do you make an activity the main activity?

Navigate to app>java>your app's package name>Right click on it>New>Empty activity and name it as MainActivity2. Navigate to app>res>layout>activity_main2. xml and add the below code to it. Comments are added in the code to get to know in detail.

How do I start my app when the phone starts on Android?

Activities: developer.android.com/guide/topics/fundamentals/activities.html Services: developer.android.com/guide/topics/fundamentals/services.html To start your application when the phone actually starts up, you'll need to register the service as shown above, and then in there you can use startActivity() to start your ...


1 Answers

This should be handled on the Application level.

For API level 14, you can register an ActivityLifeCycleCallback in your Application class

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

You can use it, to know on an Application level, which activities are destroyed, paused, resumed etc etc. Whenever, an activity is paused, without a new activity being created/resumed, you should clear the Activity stack, and re-launch your startActivity

If you target SDK versions < 14, you should implement your own method, to know which activities are created/resumed and paused, and do the same whenever an activity is paused, without a new activity being created/resumed

like image 82
Entreco Avatar answered Nov 01 '22 23:11

Entreco