How to manipulate back stack using FLAG and Activity Affinity - android

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?

Related

android Prevent a background app from launching activities in the foreground

I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle

Clear Android activity stack and start activity

I know this question has been answered in another posts also. But it doesn't solve my issue.
I have 3 screens: A,B and Home. App starts with A, then goes to B and then to Home page. But I want to delete activities A and B when it reaches Home activity, so that when back is pressed app exits.
I tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP. It works fine when back is pressed. App quits. But if I again open the app from background, it starts with screen B.
Intent.FLAG_ACTIVITY_NEW_TASK. This works exactly as I want. But when Home activity starts, there is a sudden glitch in the screen and it is not smooth.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Try the following code:
Intent i = new Intent(YourActivity.this, HomeActivity.class);
i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
You can do this way also, for your splashScreen(A) you can set nohistory=true in AndroidMenifest.xml, and goto Activity(B), and when you are going on Activity Home from Activity(B) finish activity(B) than goto Home activity.
so both of your previous activities will not store in stack.
And on Activity B override onBackPresh() method but remove super.backpresh() in side it. and start your activity A with finish this current activity, it will work.

Exclude Activity from recents and history

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

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...

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