First activity is called after coming in foreground from background - android

In my application, I am finishing my first activity when I move to the next activity like this:
if(className.equals("com.tritonhk.android.LoginActivity"))
startActivityForResult(in, 1);
//loadingrelative.setVisibility(View.GONE);
displayVal = 0;
Helper.IsFullSync = false;
LoginActivity.this.finish();
So that when I go in background from any other activity and come back in foreground then that activity must be called by which we went in background.
It is happening in some cases but If I remain in background for more then 10 minutes then my first activity's oncreate method is called.
It seems that dalvik is killing my application process that is why when I click on my application icon then its new instance is created and hence its onCreate is called.
Please suggest me what should be the better approach for this.
EDIT Problem solved partially. Now with android:launchMode = "standard" behaves normally but not in first attempt. I mean when I install the app and run it and went to background and come back to foreground then it does not work but from the second time it works properly.

Since you are finishing your current activity which launches subactivity means you don't want the task to retain root activity on re launch ,
you want to start from where you left ,you may use android:alwaysRetainTaskState which will retain state of task since it is useful for root activity only so you have to start your subactivity in a new task using FLAG_ACTIVITY_NEW_TASK intentfilter flag.

Do u have android:launchMode="standard" for the activity which is called while launching the app? If not add this line.

Related

Android - Resuming already running activity

I have an android application which has only one activity having only one button. When we press this button, another thread starts which does some particular work. If the button is again pressed, then the running thread stops.
Now the problem is, if i leave the thread running and go to the home screen and again press the launcher icon of the app, it starts a new activity. Now if i click on the button of the activity, another thread starts. Now I have two threads running at the same time, which i don't want.
I want to reopen the previous running activity and then when I click on the button again, the already running should stop.
I tried using launchmode: singleTask, singleInstance but nothing seems to be working.
Please help how to handle this problem.
Try reading the docs # http://developer.android.com/guide/topics/manifest/activity-element.html
--
"singleTop"
Conditionally If an instance of the activity already exists at the top
of the target task, the system routes the intent to that instance
through a call to its onNewIntent() method, rather than creating a new
instance of the activity.
Alternatively
you can control the thread based on the onPause()/onCreate()/onResme(),
pause in the onPause()
create/start in the onCreate()
restart the thread in the onResume()

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.

How to launch activity the same way launchers do?

I have an Activity A which is declared as singleTop and android.intent.action.MAIN in Android Manifest. I start it from launcher, then launch another activity B through Intent and then press Home button. Now I have a task with activity stack "A, B" waiting in background. If I then again start activity A from launcher I get back already running instance with a stack restored (activity B running in foreground).
This is nice. And I want to achieve the same effect when launching activity A from my own Notification. I've tried different combinations of Intent flags but I've got either a new instance of activity A or the same instance but with cleared stack (no activity B in foreground).
First of all, let's make sure that the system won't kill your Activity B when you are not using that task for a long time.
If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.
There are some activity attributes that you can use to modify this behavior:
alwaysRetainTaskState: If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period. So what you need to do is to set this to true under your <activity> tag in your manifest file for A.
Second, you won't need singleTop. Check this figure from Android docs. I guess this is what you want. If you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack.
So you also need to add singleTask under the tag of A and B, instead of singleTop. Then, you need to launch activity A from Notification without any flags but FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_REORDER_TO_FRONT(optional).
I haven't tried this myself but I have a strong belief that it will work. Try it and let me know if it doesn't.

launch external activity w/o it appearing in the back stack

I need to launch an external activity (which isn't mine) for a split second, and then I have a service that launches my previous activity for the user to see.
This works great, but the problem is when the user then presses the back key, he gets to that external activity.
I have two different activity launches here, one for the external activity, and one for my activity to return to (in which I need to use FLAG_ACTIVITY_NEW_TASK because I'm launching it from a service).
For these two launches I've tried any combination of:
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, FLAG_ACTIVITY_NO_HISTORY, FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_CLEAR_TASK, FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.
Nothing helps, I keep seeing the external activity after pressing back.
I've seen some similar questions here, but they all have the middle activity in their own app, so they can call finish on it, or set noHistory="true" in the manifest.
Any ideas?
UPDATE:
Seems like the issue is that I re-launch my previous activity from a service, which means I need to give it FLAG_ACTIVITY_NEW_TASK, from the documentation of Tasks and Back Stack:
FLAG_ACTIVITY_NEW_TASK Start the activity in a new task. If a task is
already running for the activity you are now starting, that task is
brought to the foreground with its last state restored and the
activity receives the new intent in onNewIntent(). This produces the
same behavior as the "singleTask" launchMode value, discussed in the
previous section.
And from the singleTask paragraph:
"singleTask" The system creates a new task and instantiates the activity at the root of the new task
...
Note: Although the activity starts in a
new task, the Back button still returns the user to the previous
activity.
So I need a flag that overrides this behavior somehow...

Finish activity

I have program with login, main activity and other activities.
First step is login activity(A). If login succeed start main activity(B) and call finish for (A).
working with (B) I'm calling some activities and then back to (B).
When decide to exit - I call logout and try to close (B) calling finish.
This logik works in 70% ot time :(
Unfortunately on 30% after calling finish for (B) - activity(A) appears on screen and start logging me.
Who is starting (B) again? I din't see relation between problem and program usage.
Update:
I put hohistory for (B) and start (B) with FLAG_ACTIVITY_CLEAR_TOP.
UPDATE2: Described behaviour is typical when I set screen orientation mode in code. In manifest is set portrait. When start activity I'm setting orientation depending on user config. This produced onCreaste twice. I got managed to handle this properly, but this causes problem as described. If I don't set orientation - one onCreate is called and no problem with finish.
Check the following links, you will get the solution:
Finish parent and current activity in Android
http://developer.android.com/guide/topics/manifest/activity-element.html#clear).You
You can try this when you logout in activity B. The activity displays the home screen.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Are you starting your application from an IDE (like eclipse), or from the app installer? If so, you will see this situation if you do the following.
Launch your application from the IDE or after installation from the installer (displays your first activity)
press the HOME key (takes you back to home screen)
launch your application again by selecting it from the list of available applications
This sequence will create 2 copies of your first activity, one on top of the other. When you finish the top one, the one underneath it will be shown.
You say this doesn't happen all the time. If you don't launch the app from the IDE, but just from the list of available applications you won't see this behaviour. Also, if you never press the HOME key and relaunch the app using the list of available applications you won't see this behaviour either.
When you go from activity A to activity B first time you are supposed to clear your stack top. Otherwise activity A stays in the stack below B and when u finish B, activity B is called again.
you should set in manifest file android:noHistory="true" to make your A activity not to stay on android stack.
In case you would like to do it from code in future by using intents Intent.FLAG_ACTIVITY_NO_HISTORY will do the job for you.. Cheers
Using setScreenOrientation made thinks complicated.
It is not enought to set noHistory for activityA and call activityB with FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK.
When Start app in portrait mode /as declared in manifest/ - It was OK.
But when call setScreenOrientation - have to call finishB 2 times to exit.
/Probably because ot 2 times onCreate for activityB/.
This made thinks to work:
For activityA: android:noHistory="true" and android:launchMode="singleInstance" in manifest.
Start activityB with startActivityForResult and flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK.
In activityA:
1. startActivityForResult(activityB)
2. finishA.
In activityB - when call finish() for B - because of 'singleinstance' system din't start activityA again.
Hope this help.
If anybody know reason which will cause error - please write me.

Categories

Resources