I have Activity A (Main) and Activity B.
From A, I can go to B. When I test I do the next:
Go from A (a1) to B (b1). Then go back to A (a1). The again from A (a1) to B (b2) and go back to A (a1).
If I push the back button again after doing what I said, I want to exit the app but it returns to the first instance of B (b1), and then if I push again, it goes to the first instance of A(a1) and if a push again now it exit the app.
I don't want this behavior, if I am in activity A and push back button I want to exit the app, not to go to every instance of activities until goes to the first one and then exit.
I hope I was clear.
Probably what you are doing is to stack one Activity in top of the other.
You can make sure to close B activity if you implement the onBackPressed() function and finish the activity like this:
#Override
public void onBackPressed() {
finish();
}
This way you'll return to your original A activity (and not another instance -- like A1), and if you try to go back from A you'll exit the app.
I hope it helps!
Related
[UPDATE BELOW]
I have application with multiple activities. I have issues in handling the activity started from the click of notification item.
Below is the app structure:
For normal flow/working, user start the application and Activity A being the launch Activity, it starts. User could navigate to activity B from activity A (i.e. A -> B)
On click of back button on B, activity A is displayed.
However, the issue is with starting activity from the click of notification. Below is the flow:
User clicks on the notification
Activity B is started
On click of back button on activity B, it doesn't start activity A (which is understand) but it doesn't kill the activity B either (moves activity B to background). What I want is, if the activity B is started by the notification click, then back press on B should either takes user to Activity A or kill activity B.
Any pointers in this regard would be appreciated.
[UPDATE]:
Strange but, this is what is happening now.
1. Overridden the onBackPressed event in activity B.
2. In this method, finish() is called.
If the application is already running and on click of notification, activity B is displayed and then on back press, the activity B is finished. However, if the application is not running, notification click starts the activity B and on back press, it does call the backpress event and finish() method but the activity doesn't finish and goes in the background. This is really weird. Or may be I am missing some details :((
Thanks
Shrey
Override in onBackPressed().
#Override
public void onBackPressed()
{
finish();
super.onBackPressed();
}
But, user can directly got to activity B, if he/she start's your app from recent apps. To avoid this, add below to your ActivityB in Manifest. If you are navigating to ActivityC from here, pressing back in ActivityC will not return to ActivtyB so override onBackPressed() and relaunch ActivityB via Intent.
<activity
android:name="ActivityB"
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:noHistory="true"/>
follow this flow:-
1- check isLauchFrom in oncreate() of Activity A.
2- If isLaunchFrom Notification then navigate on Activity B.using startActivityFor Result.
hope this will help you ,If any Confusion send comment.
#Override
public void onBackPressed() {
Intent returnIntent = new Intent(B.this,A.class);
B.this.finish();
startActivity(returnIntent);
}
Try like this
I have two Activities, A and B. Here is a normal scenario: A is running, then sends an intent to B. A is paused, and B displays. When the user presses the back button from B, B us destroyed and the user is returned to A.
However, there is a case where B needs to re-create itself. To do this, I call finish() and then startActivity() on B and that works fine. But then, when I click the back button, it shows B again, and so I need to click the back button once more to get back to A.
How can I re-start B, but still be able to press the back button only once to return to A?
The following will dispose of the current activity while launching the next intent:
Intent launchNext = new Intent(getApplicationContext(), NextActivity.class);
launchNext.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launchNext);
To override the back button, use the following:
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish(); // or do something else
}
This can be solved by taking a closer look at your intent flags. Check out http://developer.android.com/reference/android/content/Intent.html and they give more information about what lifecycle you are shooting for.
Also, don't forget that you can override the back button functionality.
This may be helpful in case you want to manage your life cycle more closely.
For example, you can also make sure to go back to A if back from B. And close your app if back on A.
I have this issue.
I have an actvity A that starts other activity B (by onclick - button).
In B I have one back button to come back to activity A. I press it.
Now that I'm in A, I press again button to go to B.
If I use android back button (I'm in B) I come back to A first and then to B.
But now, if i press android back button again, I don't go to previus activity of A or it exit from app. I come back to B !!!
How can I prevent this behavior ?
The back button of activity B should not start activity A, but close activity B with finish:
http://developer.android.com/reference/android/app/Activity.html#finish%28%29
Note that it is probably bad user interface design to have a "back" button on the interface. This official Android page says "Don't use labeled back buttons"
http://developer.android.com/design/patterns/pure-android.html
if you wish your custom back button to swap between the acivities A and B, you should simply launch intents, that will do.
I believe your problem is with the android back button. If you do not want go to the previous activity with this action, you can write in the manifest file under the <activity> tags for both A and B activities - android:noHistory="true". Doing this will exit from the app. as the activity stack was storing none of the activities, but such a requirement is quite confusing UI approach.
I have an application which consists 3 activities - lets call them A, B & C.
From activity A You can get to activity B, from B to C and from C to A (By pressing the back button).
On activity A I have an exit button (I know it`s not recomandded on android, but a lot of users are asking for it, so I decided to add it).
Anyway the problem is that: activity B has a timer, when the timer is finished it starts an intent that starts activiy C and calls finish() on activity B.
When the user then press back on activity C he gets to activty A - and then when he presses the exit button (this button only preforms finish()), it pops up activity B again. How can I prevent it from happen?
Thanks!
Idea: set a isFinished field in your Application to true and call finish(). All activities check if isFinished in their onResume() and if true they finish().
I'm not sure I understand how you end up at A when going back from C. Shouldn't it go back to B (logically, I mean. I know you want it to go to A)? In fact, that seems to kind of be what's happening. B is getting relaunched, but A appears on top of it, so that when you finish A, B (which was under it) is now shown.
It sounds like you don't want B to be part of the history stack, so maybe when you launch B, you should launch it with the no history flag.
You activity b is not finished thats what i can figure out it is still in running state so when all other actitivties are finished it shows up please chekc if it is finished or not
I have two activities A and B. What I want is to show activity A as an intro, the user will be unable to navigate back to it. Is there some flag I can set to activity A to do this? Can I block the back button for one activity only? Activity A is of course my main activity which automatically starts activity B after some "hard work".
Best regards.
you do not need to block the back button, but just call finish() on your A activity after firing an intent to start B. Back button pops the previous activity from activity stack and it won't be able to pop A if it is already finished.
For this you don't need to block the Back button. Simply, start the second Activity and quit the first one. And now if user presses the Back, they will be taken to the Android home screen not on your apps home screen.
Updates: By the way if you want to intercept the Back button for any reason, simply override the onBackPressed() method of Activity class. See this for details.
Never override the functionality of a hardware button.
You should call finish() in Activity A right after starting Activity B (calling the Intent).
it works but the application terminates and i'm redirected to android's applications screen. I would like to stay in activity B if back button is pressed, i don't want to exit the app. here's is what i got :
public void startProgram(Context context){
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish();
}