[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
Related
I have two activites. Activity A and Activity B.
Activity A is a singleTask activity. Activity B has no extra configuration. So it is a normal activity.
When Activity A launches the activity B then if user presses to the home button then comes back immediately (so my app is not killed by android yet.) My app is showing activity A. But it should show the activity B because the user pressed to home button when active activity is B.
If i remove the singleTask property from Activity A. It is working as expected. But i have to use singleTask activity for some other reason.
How can i resolve this problem?
As you have constraint to use singleTask launch mode, you can do following :
When user press Home, app or Activity B goes in background (stopped state)
Then when you open app, it'll show Activity A because of singleTask launch mode
In Activity A, override onNewIntent() method. This will get called from android os. You can fire Activity B's intent from this method. So, system will open Activity A when coming from background to your app, but from this method, Activity B would be shown.
I've edited my answer to handle below scenario :
If we're on ActivityA, we press Home button and we come back, ActivityA should be shown, not ActivityB. To do this, I used boolean shouldOpenB
in Activity A :
static boolean shouldOpenB = false;
// set shouldOpenB to true when you are opening ActivityB from your code with Intent
// reset shouldOpenB to false in onStart() of AcrivityA
protected void onNewIntent (Intent intent) {
if(shouldOpenB) {
Intent openIntentB = new Intent(this, ActivityB.class);
startActivity(openIntentB);
}
}
Note : The animation of Activity A -> Activity B would be visible to user for fraction of time. If you agree to change to singleTop, we can remove this animation also.
I have activity A->B in the stack, and to launch activity C, I call
Intent starter = new Intent(context, MainActivity.class);
starter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(starter);
This all works fine, Activity A and B both have OnDestroy called. If I press the hardware 'back' button now, the activity appropriately finishes and is hidden. The problem is now however, if I return to the application through the application by clicking the hardware recent apps button, it will return to Activity A. Activity was destroyed and not in the stack. In the manifest, none of activities have had a android:launchMode set, so they are on default.
The only other possible piece of relevant information is that there is an Activity X that is a launcher Activity that is android:launchMode="singleInstance" and it launches activity A, that being said, it gets destroyed and it shouldn't be in that activity stack anyways.
While pressing back button While in Activity C may called onDestroy() of actvity C.
please insert logs to see whether it is called or not. This is the only reason why your activity A launch again.
please refer Android Back button calls ondestroy?
please let me know if these not work for you.
The hardware back-button can be overwritten by the follwing code :
#Override
public void onBackPressed() {
//put Intent to go back here
}
You could just overwrite it with your code written above
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!
I have an application that starts activity A. The user can then start service S from activity A. When a certain event happens, service S starts activity B. Activity B only has one button, and when pushed, should return to activity A. Everything works fine, except for when activity A was closed out using the back key. When the back key is pressed, instead of onPause, onDestroy is called. So when activity B is dismissed, I get sent back to the homepage instead of Activity A. So my question is, how can I make sure that Activity B reopens activity A if activity A is onPause, and also reopens the activity A when it's onDestroy has been called. I'm thinking to use intent filters, but I can't figure out to get the right combination. I do not want multiple instances of Activity A. Thanks for the help.
on activity A:
public void onBackPressed() {
moveTaskToBack(true);
}
or on Activity B, force it to start activity A,
public void onBackPressed() {
Intent i = new Intent(B.this, A.class);
startactivity(i);
}
but since you implement the first one,
you dont need this last code.
I Hope it helps
Since you don't need to save A state,
you can override onBackPressed() method in B to start A again.
And if you don't want multiple instances of A, use FLAG_ACTIVITY_CLEAR_TOP when you start B.
Hope this helps
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();
}