I am developing a small application at the moment it consists on 3 Activities.
Now when I start my application it starts fine and I can navigate from activity1 to activity 3 properly and without any problem.
Activity1-->Activity2--->Activity3
The problem comes that when I press the back button of my mobile device to go back to activity2, the application simply closes.
Can somebody please suggest how to figure it out what is happening.
that is how i am going to Activity2 from Activity2
Intent activity3 = new Intent(Activity2.this,Activity3.class);
Activity2.this.startActivity(activity3);
Activity2.this.finish();
Note:I am not using emulator I am using mobile and doing all the debugging directly on the mobile.
Thanks
This is because you are calling the finish() function which removes the activity from the stack. Remove the line Activity2.this.finish(); and you should be going back the way you wanted.
Because you call finish() method.
Due to this from activity stack your last activity is removed so that's way your current activity is finished on backPressed().
To overcome this You must remove
Activity2.this.finish();
from your code.
Related
I have 3 activities ( say A, B, C were A-is the Launch activity). When I press home button when at activity C, app goes into background. After that, I took my app through all apps list menu. At that time, my launch activity is showing (activity A). When I press back button, It goes to previous activity (C). I want to retain in the same activity (C) while coming back from background. When I run application through Eclipse, It works fine. But when I send Apk file through mail and run it in device, It fails (previous problem occurs ).
I tried with
android:launchMode="standard"
AND
android:alwaysRetainTaskState="true"
in my launch activity (Login activity or A). Any body please help me, Thanks in advance.
Follow following steps to insure that you are following the right practice:
1. Make sure you are using finish(); on the Activity A or B if you want to finish it and dont if you want the back button functionality.
2. Try Implementing onpause() and onresume() even if you are not going to perform any functionality in them. Use super() for them there.
3. Also, in android when you start an activity by clicking on the icon instead of resuming it from already running activities, it exhibits a different behaviour.
I have an issue with my Robotium tests. On one of my Activity A, I am clicking on a button. Clicking on this button launches another activity B. So in my robotium test I have something like this :
Button myBtn = (Button) solo.getView(R.id.myBtn);
so after this action, the emulator is automatically launching activity B. Now, the problem is that I do not have any possibility of going back to the activity under test (A) in the code.
Can you please tell me how to avoid getting jailed in the activity B when clicking on the button that trigger its launch ? In other words, is it possible to go back to the activity under test ?
FYI : I need to go back to the activity under test because there are other test methods waiting to be fired.
thanks in advance,
Please try methods like
solo.clickOnView(R.id.myBtn)
to click on your button
You can also try
solo.clickOnButton()
To go back to activity you can use
solo.goBack();
or
solo.goBackToActivity("ActivityName");
Once you call any activity outside your application, it’s not possible to come back to your application using solo.<any API>, because solo is tight to UID of your application and doesn't work on other app's activity (with different UID).
Basically solo can only work on activities which belong to the application for which it’s created.
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.
I have two Activities FirstActivity and SecondActivity.
FirstActivity has an intent filter MAIN, LAUNCHER and DEFAULT.
SecondActivity is just a normal activity and no other flags is set in the AndroidManifest.
My application is C2DM enabled and when a notification is received a taskbar Icon is displayed and when clicked it opens SecondActivity.
Arrival of Notifications has two scenarios:
First, My Application is Already Running when notification arrived and second My Application is totally not running.
On the first scenario, everything is fine. A notification is received, i clicked from the notification drop down and my SecondActivity is displayed. When I press back SecondActivity is finished and application goes to the background. When I start my application again from Recent Applications, it displays FirstActivity which is correct since its the MAIN, LAUNCHER and DEFAULT.
On the Second Scenario, A notification is received, I clicked it from the notification drop down and my SecondActivity is displayed. When I press back SecondActivity is finished and application goes to the background. When I start my application again from recent Applications, it displays SecondActivity which is wrong. I am expecting FirstActivity to be displayed because it should be a fresh start.
I tried ForceKilling the application after the second scenario but the result is the same, I needed to restart my phone to be able to start from FirstActivity Again.
Have you guys encountered the same problem? or is it just me? What do you think is wrong with my configuration?
I also tried setting noHistory=false to SecondActivity but still the results are the same.
When you start application from 'Recent Application' shortcut, it will launch the last activity. So you can try to launch the application from the application list, see whether it solves your problem.
It is the correct behavior.. if you start the application through "Recent Application" it will point from the last activity.
Try launching it through normal application list
For your main Activity try to set the lauch mode as "singleInstance" in the AndroidManifest file. for second activity set "Finish On Task launch" to true.
In Second scenario i think activity stack is empty as app is closed . so now you are starting activity2 , means this is the only activity in stack . now desired back behavior can be possible by overriding onbackpress and start activity1 if its second scenario .
you can call your second activity with the flag set no history like this,
intent1.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
This has worked for me several times.
Read from the Android Doc
The problem is somewhat odd and after having trying to figure it out for about a day now, I am posting it here.
I have an application where an activity A(main activity) launches other activities(B,C or D).
The issue here happens when activity A has started Activity B and 'home' button is pressed.
Case 1 - When I test my application in debug mode on my device (HTC Desire) after pressing the 'home' button, I again click the application icon, it returns to the same activity (activity B), which is what is should do. No issues here.
Case 2 - When I export the signed package, and then install the application on the same device, then if I click the application icon after pressing the 'home' button, then a new instance of activity A (main activity) is launched ON TOP of activity B. I got to know this because when I press 'back' from that activity, it returns to activity B and pressing 'back' again shown activity A.
The behavior ceases to exist if the application is quit in the same order it was started, that is, if I press 'back' from activity B, then 'back' from activity A (exit).
After this everything runs fine.
I have tested it many times with different settings but I can't seem to figure out why the behavior is like this.
Any help is appreciated.
I think giving Activity A the 'single top' flag in your manifest should fix this.
Regarding Case 1:
When launching your intent from Activity A to start Activity B, add the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
This will ensure that when you go home and launch the the app again, Activity A will be shown.
Regarding Case 2:
I'm not exactly sure how this would occur. It seems like it thinks you have two versions of the app, the signed one and the unsigned one but keeps them both in the same task stack. You may want to consider using singleTask or singleInstance for your Activity if you only want one instance. See the doc on tasks and back stack for more details.
I would agree with Noel regarding the likely cause of Case 2. Without task reparenting or it being set to a launchmode preventing multiple instances of an activity, there is a chance that launching it from Home isn't deemed the same stack as launching it from Eclipse (assuming this to be the case).
In my talent calculator app I have the whole application set allowTaskReparenting=true to ensure nothing is left in other stacks (primarily email as it can email launch urls). I then have my main activity set to launchMode="singleTask" as I only ever want one instance of this to exist no matter what launches it or with whatever intent.
My only other activity is for loading and saving and that has noHistory="true" to make sure it is removed and never returned to. That basically means it only exists while you're in it, and can never return to it.
clearTaskOnLaunch="true" will also ensure only the main Activity remains in the stack when it's launched from Home, but this isn't always the case if you have other ways to get into your activity. If it's only ever launched from Home then set this.
Hope that all helps.
Do you start you application manually or using Eclipse or another IDE? When starting from Intellij IDEA I had exactly the same problems. Then I stopped and ran it manually and behaviour was as expected.