here is the brief of my activities actions :
Activity A is a launcher that call activity B with intent flag Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK.
When Activity A launch activity B, ondestroy is called in activity A, as expected.
When I press back button in activity B, ondestroy is called in activity B and it bring me to the hope launcher.
There are no alive activity at this moment, but the application is still visible and the task manager. When I click on it, it brings me to the activity A (maybe because it is the action MAIN in the manifest).
The behaviour I try to get is that when i press back button in activity B, it brings me to the home screen and I can return to is, as if it is root activity.
Can anyone help me for this ?
Thanks you !
If you click on the Back button in activity B, you will be able to navigate to the Home screen and return to the root activity(A) from the following sources.
why you used Intent.FLAG_ACTIVITY_CLEAR_TASK?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.btn_send)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Case 1.
//startActivity(new Intent(A.this, B.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
//Case 2.
startActivity(new Intent(A.this, B.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
}
});
}
FLAG_ACTIVITY_CLEAR_TASK
added in API level 11
int FLAG_ACTIVITY_CLEAR_TASK
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
Constant Value: 32768 (0x00008000)
Google Developer
You have to remove the flag FLAG_ACTIVITY_CLEAR_TASK, because as the android developer site here, the behavior will destroy your Activity A,
You need to remove that flag and then your activity will remain in the stack, and you will be able to back to activity A
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 want to go back to an already launched activity without recreating again.
#Override
public void onBackPressed() {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
this.startActivity(i);
}
Instead of doing is there any other way to go back to another activity which is already created, which I get back with " Back button "
Add android:launchMode="singleTop" in the FirstActivity declaration in your AndroidManifest.xml like so
<activity
android:name=".FirstActivity"
android:launchMode="singleTop"/>
You could also try the following if SecondActivity was started from FirstActivity
#Override
public void onBackPressed() {
this.finish();
}
Whenever you start a new activity with an intent you can specify an intent flag like this:
// this flag will cause the launched activity to be brought to the front
// of its task's history stack if it is already running.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
or
// If set and this intent is being used to launch a new activity from an
// existing one, the current activity will not be counted as the top activity
// for deciding whether the new intent should be delivered to the top instead
// of starting a new one.
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
When starting SecondActivity do not finish the first activity so when you go back , you will be taken to the first activity.
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
On your second Activity, close the Activity calling finish() instead of creating a Intent for the other screen.
By calling finish() the Application will go back in the Activity stack and show the previous Activity (if its not finished()).
Don't call finish() on 1st Activity, when you start 2nd Activity. When back is pressed on 2nd Activity, it finishes itself and goes back to 1st Activity by default. onCreate() is not called for 1st Activity in this case. No need to override onBackPressed() in 2nd Activity.
However, Android can destroy 1st Activity when you are using 2nd Activity, to reclaim resources, or, if you have checked Don't keep activities in Developer options. In such case you can use onSaveInstanceState() to save data and onRestoreInstanceState() to use it again in 1st Activity. You can also get the saved data in onCreate(). Here is how it works. Also, onSaveInstanceState() is called only if android decides to kill the Activity and not when user kills the Activity(e.g. using back button).
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
[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