Best Place to Put "Auto Start" another activity code? - android

My Android application(Application A) launches another application (say Application B) upon click of a button.
Now I want to implement "auto start" functionality wherein Application B will be launched as soon as Application A is launched.
For this, I created a checkbox and used SharedPreferences to store the value of checkbox.
Then, in my onCreate() method of Activity A, I am checking the value of checkbox from SharedPreferences and launching Application B in case the value is "true".
The Problem:
The problem I am facing is, when the user exits "Application B" (and comes back to Application A) the onCreate() of Application A is getting called again and Application B opens up again. This sets off a infinite loop and at exit of Application B, user returns to Application A and goes to Application B again.
I know onCreate() gets called multiple times (when we change orientation, keyboard opened, Activity goes into background and is killed by system), but is there any clean way of doing this?
To reiterate, my requirement is to launch Application B from Application A if the "auto start" checkbox is checked in Application A.

My suggestion will be to use the method onPause of the activity in application A and set a flag there "application B was called". Then if this flag is set do not call application B in onCreate of the activity in Application A and unset the flag.
If application B is too long in the foreground application A might be suspended by the system and the flag will be reset. In such case maybe it is good idea to have the flag stored in some persistent storage (e.g. SharedPreferences).
EDIT One more thing: the flag should be set in onPause only if the activity is being paused, because the other application will be shown (this will be easily determinable, because everything happens in the same class).

Related

How to resume activity when application relaunched from android launcher?

I have an application that implements two activities. Activity A is for selecting some files on device and activity B is to show additional info while these files are processing. Both of them have singleInstance as launch mode.
On application's start activity A runs. Then this activity starts second activity B, that creates a notification. If I tap this notification or open running app from recents, it works fine and shows running activity B. But if I'd started application before and activity B is already running, launching it again from app menu causes showing activity A when old activity B is already running and accesible from notification bar.
So, what should I do to make application run only single activity at the same time and show second activity when called from launcher (if second activity once started and isn't finished)?
Your launcher intent should be mapped to a dummy activity, with no view (or may be a splash who knows !). In onCreate of that dummy activity you check if a state was saved previously and based on the state switch to the desired activity.
And how to set the state ? well lets try something simple. lets say when Activity A resumes we put "A" to some key in SharedPreference. if Activity B resumes we put "B" to the same key in preferences. Now your application goes out when Activity B was visible, the state saved would be "B", you launch from home screen, the dummy activity finds "B" in the state and launch intent to go to B else go to A.

Android: Get initialization code to run every time user launches app

I have an app with a main activity (A) with menus, and separate activities (B, C, D) for the tasks selected from the menu. I have initialization code which is currently in onCreate(). But if the user leaves the app by pressing the home button, and then re-launches by tapping the app icon, onCreate() does not run. I cannot put the initialization code in onRestart(), as this runs when the user returns to the menu after a task run by say B. How can I get the code to run on every launch, but only on launch?
First you need to understand how the android life cycle works:
http://developer.android.com/training/basics/activity-lifecycle/index.html
Basically, you need to run your code on onResume and onStart depending on what you want to achieve
EDIT:
Since the icon launches a VIEW intent, you could check for the intent when the application is resumed or restarted.

Start different activity on application restart

I have an application which has two activities A and B. When application is installed and run for the first time then it always starts with A as it is declared as launcher activity in manifest.
According to workflow, after a few seconds, activity A is destroyed and activity B is started. So that, the root of task becomes B. Now, when user presses the home button and later comes to our app, activity B is resumed as expected. But, if application is left in background for a long time then the application is restarted as from the logs Application::onCreate() is called. However, I want that whenever the application restarts that is whenever Application::onCreate() is called (like in the second case) activity A should be started instead of B, in all other cases when application is not restarted activity B should be appearing. I am new to android and unable to figure out a solution. Any help is much appreciated.
Thanks in advance.
Use SharedPreferences : http://developer.android.com/reference/android/content/SharedPreferences.html
In the end of your activity A you should create a preference that save the a state of your app in the SharedPreference.
at the start of onCreate() of your activities check the state and start the correct activity.

Android; How can I detect whether a Parent activity is still alive in Activity stack and then receate it

I have activities in sequence as Activity A calling Activity B,When I am on Activity B I press Home button and start another Application (for example Map). If i stay on this second application for a long time say 5-10 minutes and then press Home Button again and choose to Start my application again, then Activity B is started again and works fine. But when i Press Back key - I return to my Activity A again (which is also correct) but it shows a Blank Screen. Ideally in correct version it should show me Acitivty A with the XML data is ListView form.
Alternatively, in the above description, when the other Map Application is started and if the user stay on it only for 1-2 minutes then the above problem doesnt not occur.
Can anyone suggest a solution on the same.
Is it that i need to check in Activity B whether Activity A is still there in Activity Stack (How do i do the same) and If its not in my Activity stack then re-create it.
I tried doing alwaysRetainTaskstate in my Android manifest file for Activity A. But it doesnt work at all
You don't have to do any of that, Android takes care of the technicalities for you - it will recreate your Activity A.
You just need to remember to save A's state when B is opened (take a look at Activity.onSaveInstanceState). When A is restarted, your saved state will be passed to onCreate.
You should really read about Activity Lifecycle
This should help.

Adding an activity to the stack when the application is in the background

Suppose the user is in the activity A of my application.
The user leave the application (with the home button) and at some point, while he is doing something else, I want to change the stack of my application to A B. I do not want the activity B to pop up from nowhere, I just want that when (if) the user returns to my application, he sees the activity B.
It seems that calling startActivity(B) in the activity A from a background thread works, but I’m not sure this will have the desired behaviour on every platform (what I want is that the user does not see the activity B until he returns to my application)
I may not be following this correctly
If all you are after is saving state (which activity was last active) you could use preferences Shared Preferences
You could then just have a MainActivity that does nothing other than decide which activty should be displayed the next time your application starts
Main
Using preferences find the last activity that was active
Start first activity dependent last activity ID or whatever logic you choose
Activity A or B starts/resumes etc - Store activity ID in shared preferences for retrieval later

Categories

Resources