Exclude Activity from recents and history - android

Actually I have a direct issue with android custom tabs.
But nevertheless I will generalize my question. Let's assume that I have ActivityA and ActivityB. ActivityA - is an external one, so that we launch it from an Intent :
Intent intent = customTabsIntent.intent;
intent.setData(requestUri);
intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE);
mContext.startActivity(intent);
From here we see that ActivityA - is actually a custom tab activity. When things get done I launch ActivityB. I want ActivityA dissaper from task history, I can achieve this by aplying flag Intent.FLAG_ACTIVITY_NO_HISTORY to my intent. But this approach is causing problems, because when user switches to recents,or goes to other app - ActivityA disappears forever. Of course - that's how flag no history works. But I want a different flow, I want ActivityA disappear only when ActivityB was launched. Any ideas how to achieve this?
P.S. I need to finish ActivityA, which gets launched via intent, I don't have access to its code and I don't have the ability to call finish().

I don't think you can do this by using the NO_HISTORY flag since ActivityA is doing the launching and you have no control over it.
However, you should be able to achieve your goal (not being able to go from ActivityB back to ActivityA by overriding the BACK button in ActivityB and having it go back to whatever is underneath ActivityA in the task stack.
Assuming that ActivityC is the one that starts ActivityA, you could do something like this in ActivityB:
#Override
public void onBackPressed() {
// Direct user back to ActivityC
Intent intent = new Intent(this, ActivityC.class);
// Add flags to ensure that we reuse the existing instance of
// ActivityC and that we clear any other activities above ActivityC
// from the stack.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}

You can use finish() method to clear any activity. so before starting another activity finish that.
Intent intent = customTabsIntent.intent;
intent.setData(requestUri);
intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE);
mContext.startActivity(intent);
finish(); //add this to clear that activity

Related

Android - Keep bunch of activities in each task

I have few activities.For example A,B,C,D,E,F.
My Home screen is activity A.Now I start Activity B from that and C from Activity B.
So now my stack is A->B->C.
Now if I press back I should be able to navigate in reverse Order.
But If I start Activity D from C.
I want my stack to be A->D as I want to kill B and C.
My expectations is A is my Home screen which should be always there.B and C on having one task and D,E,F having other task.
There is one more catch - I can even start activity D->E->F from A and B from F. In that case when I start B from F, D,E,F should be removed from stack
There are several ways to accomplish this, depending on your application requireents. You should look at using startActivityForResult() in order to start one (or more) activities which can then return a result to the Activity that launched it. This may be a good solution for you.
The other alternative is to use your ActivityA as a kind of "dispatcher. In this case, when C wants to launch D but also clear the task stack back to A first, you can do something like this in C:
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("launchActivity", "ActivityD");
Using the flags CLEAR_TOP and SINGLE_TOP will cause all activities on top of the existing instance of ActivityA to be finished and cause the Intent to be routed to onNewIntent() of the existing instance of ActivityA.
Now, in ActivityA, override onNewIntent() with something like this:
if (intent.hasExtra("launchActivity)) {
if (intent.getExtra("launchActivity").equals("ActivityD")) {
Intent launchIntent = new Intent(this, ActivityD.class);
startActivity(launchIntent);
} else ...
}

How to manipulate back stack using FLAG and Activity Affinity

I have gone through android docs on FLAGS and Affinity (Link)
and tried different combinations that I thought would help achieve my requirement but it seems that I'm missing something.
I need to initiate activityA from from BroadcastReceiver and then finish activityA and start activityB. so when the user presses back it wont go back to activityA; when user presses back on activityB it will stop the activityB and it will go to back stack however when the user opens app back from task manager it will start up from activityA.
Note: this does not happen when there is another activity of the app in the background.(only happens when the app is entirely force closed/killed).
also when user presses home button in activityB it behaves as intended.
I use the following code to lunch activityA from BroadcastReceiver.
Intent intent = new Intent(context, ActivityA.class);
intent.putExtra(EXTRA_TAG, new Gson().toJson(remoteMessage));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
I use the following code to lunch activityB from ActivityA.
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
finish();
Is there a different FLAG that I need to use to achieve this requirement?

Android - Activity Hierarchy (back button)

I have the following model:
MainActivity navigates (through a button) to ActivityA.
ActivityA displays list of records as a ListView (from SQLite database).
If I click on any record, an intent will start ActivityB which displays the data of the selected record to be edited.
If I press on the device back button it will take me back to ActivityA.
ActivityB has a 'save' button, which if clicked will open new intent of ActivityA with the updated records (after edit), also, this button will finish() ActivityB.
Here if I press the device back button (after saving ActivityB and opening ActivityA), it will take me to the old ActivityA with the data before update (the older version of ActivityA), then on the second click on device back button, it will go back to MainActivity.
The question is: How can I let the device back button always go back to the hierarchical parent, instead of going to previous open activities?
OR
How can I kill the old instance of ActivityA if the user clicks on the save button in ActivityB?
Your help is appreciated.
In ActivityB, after the save, you should call finish() instead of opening a new instance of ActivityA. You should have ActivityA re-query and update the ListView.
You can specify FLAG_ACTIVITY_CLEAR_TOP flag in the Intent used to launch ActivityA.
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
If ActivityA being launched is already running in the current task, then all of the other activities on top of it will be closed including ActivityA and ActivityA is restarted with the new Intent. There will be no need to call finish() on ActivityB explicitly.
Just add finish() in OnPause() of ActivityA and
add this method to ActivityB.
#Overrride
public void onBackPressed(View v) {
Intent MainActivityIntent = new Intent(ActivityB.this, ActivityA.Class);
startActivity(MainActivityIntent);
super.onBackPressed();
}
just try this, it really works...

How to remove all activity in Stack when press back button

I have a list of activities A - B -C -D - E and more, for example final activity is K. I want clear all these activities in stack when i press BACK button. How can i do ? In fact, i over ride
onBackPress(){
moveTaskToBack(true);
finish();
}
but only current activity is deleted and application exit. Then, i come back application, it resume activity before K. I want it start from begining when i re-open app. I think the reason here is because the list of activities in stack still are stored, so i want to clear all stack when clicking BACK button. Any suggestions ? thank you very much !
There is method called finishAffinity() for finishing all activity.
public void onBackPressed(){
super.onBackPressed();
this.finishAffinity();}
You need to call your activity with the FLAG_ACTIVITY_CLEAR_TOP inside your onBackPressed
#Override
public void onBackPressed()
{
Intent it = new Intent(YouCurrentActivity.this, YourFinalActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
Hope it Helps!
Either use the noHistory flag in the manifest or finish each activity yourself when the user navigates away.
startActivity(myIntent);
finish();
Another solution, maybe the best, if you have so many overlaying Activities: use only one Activity and handle the content in Fragments. This way you are in control what exactly you want to show when the user hits the back button.
In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.
Add this code on your onBackPressed() method,
> This launch mode can also be used to
good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to
start the root activity of a task, it
will bring any currently running
instance of that task to the
foreground, and then clear it to its
root state. This is especially useful,
for example, when launching an
activity from the notification
manager.
So your code to launch B would be:
Intent intent = new Intent(A.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity

Not to destroy current activity on back button is pressed

I have two activities, A and B. I have a button in A,when pressed starts a new intent to B.
However whenever I press the back button while in B, than click the button again is restarts Activity.I do not want to do that,I want it to resume Activity B.
Actually I am doing some networking in Activity B and I want to save unless the user wants to refresh.
How can I do it? Thanks in advance
Use
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
Hope, It must help you
You need to Overrider the
onBackPressed
method and start there the activity like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, activityA.class);
startActivityForResult(intent, PICK_CONTACT_REQ);
}
This sounds like you need to rethink your architecture. You say:
Actually I am doing some networking in Activity B and I want to save
unless the user wants to refresh.
If this is the case, you probably don't want to do your networking in ActivityB. Maybe you should start a Service from ActivityB to do the networking. The service and the activity can communicate with each other so that the activity can keep the user up-to-date about the state of the networking. If the user presses BACK in ActivityB, it can finish (as usual) and return to ActivityA. In this case, the Service is still managing the networking. When the user again starts ActivityB (from ActivityA), the new instance of ActivityB can communicate with the service to see if there is any networking going on, and if so it can get the current status of that networking or start it or stop it or whatever.
I guess I'm too late but I had a similar problem.
I had two activities A,B and a next button in A.
Whenever I tried to do: A->press next button ->B->press back button->A->press next button->B, B screen got destroyed when I pressed the back button. So when I came back to B for the second time it was a newly created B (all the information I had put in was gone).
So it was like A->B->A->new B when I just wanted to go back to the original B! :(
So what I did was, in activity B, I overrode the onBackPressed() function so it doesn't destroy the activity. I also set the flag so that if there is a A activity already running, it would just pull it up to the front instead of creating a new A activity.
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Then, for the onclicklistener function for the next button in activity A, I set the flags similarly.
public void onClickNextbutton(View v){
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}

Categories

Resources