The question is simple: Is there an intent FLAG that works exactly like pressing the back button? (making the activity slide from the left)
I need an intent to work exactly as moveTaskToBack(true) (if previous activity is in task stack).
I have tried calling Activity A (Main) with FLAG_ACTIVITY_CLEAR_TOP, but it doesn't get it "from the stack". I think it's creating a new instance, cause if I press back, I get the (on purpose unfinished) Activity B.
Note: I DON'T want to finish Activity B cause I need to go forward to it in some cases, without loosing the already loaded data.
You might want to call Activity B with FLAG_ACTIVITY_CLEAR_TOP if you want that to be on top of the stack and have A off of the stack.
If you never want Activity A to remain on the stack you can put in your manifest:
<activity
android:name=".ActivityA"
android:noHistory="true">
To go up to Activity A from your menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, ActivityA.class);
return(true);
}
return(super.onOptionsItemSelected(item));
}
If you are using you have to put:
android:launchMode="singleTask"
in manifest for your Activity B.
Hope this helps.
Related
I want to move from activity A to activity B, and there, move to A again.
A is the parent activity, defined in android.manifest in that way:
<activity
android:name=".ui.map.view.AActivity"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
While B is defined like this:
<activity
android:name=".ui.addwifi.view.BActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
I use this call to move from A to B:
public void openBActivity(UserData user) {
Intent intent = new Intent(getApplicationContext(), BActivity.class);
intent.putExtra(BActivity.SSID, BActivity.getWifi_ssid().replace("\"", ""));
if (user != null) {
intent.putExtra(BActivity.LATITUDE, user.getLatitude());
intent.putExtra(BActivity.LONGITUDE, user.getLongitude());
intent.putExtra(BActivity.ZOOM_LEVEL, (int) user.zoom);
}
startActivity(intent);
}
And from B to A:
public void openAActivity() {
Intent intent = new Intent(context, SpotsMapActivity.class);
startActivity(intent);
}
The problem is when I press back in A, it moves to another Activity A not exiting the app. What I want to do is to move from A to B, next from B to A and from A to exit (pressing back button).
I have read Google docs about back navigation but it doesn't work for me.
For one, you are working with Activity here and no fragments are being used. Maybe you can use Fragments(?) as then when you press Back, you are basically exiting from the Activity (if you're not adding Fragments to the BackStack). That will give you the behavior you need.
With you're current implementation you need to exit from the app when you press back from Activity B correct? In this case, you need to close out Activity A when you move away from it to B. A simple call to finish() method will do it. Put it after the startActivity(intent) call:
startActivity(intent);
finish();
This should close out Activity A and then start B. This is kind of like a brute shut down of A. To make it elegant, just make sure you close out any resources A may be hanging on to in order to avoid leaking.
Just as a side note, Android's expected behavior when you start a new Activity is to put the other Activity in a stopped state but still in the Activity stack. Since you are moving away from Android's design principle, maybe a Fragment may suit your need better - I don't know what exactly you are trying to do but I strongly encourage people to reconsider before moving away from Android's design.
EDIT
It looks like you are starting Activity A again from B when you are exiting B - it's not required actually. Android will retain the previous instance of A which is why you are seeing another instance of A when you go back
I solved the problem using the FLAGS. I think it is the most elegant way.
public void openAActivity() {
Intent intent = new Intent(context, SpotsMapActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
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
I have implemend the up button according to Google recommandations found here:
http://developer.android.com/training/implementing-navigation/ancestral.html
Like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// This is called when the Home (Up) button is pressed
// in the Action Bar.
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The big problem with this code is that the MainActivity is started from a new instance. I would like to open the previous Instance of MainActivity without restarting it.
As this ACtivity is quite heavy, I do not want to create a new one every time the user press on the up button.
I tried to remove the flag FLAG_ACTIVITY_NEW_TASK and played a lot with all imaginable flags, but I did not succeed to create a button that would bring back the MainACtivity in previous state.
Any help would be highly appreciated.
What you're looking for is the flag:
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
Which will do what you want - if the activity already exists in the task and stack, then it will be brought to the front, otherwise it will be recreated.
Bear in mind that if the Activity has been destroyed by the GC, then it'll be recreated anyway, and some UI components may be reset depending on where you've got intialisation code (I don't think that onCreate() is called, but onResume() definitely will be
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);
}
I have this Activities sequence:
Avtivity01 => Avtivity02 => Avtivity03
You can go from Avtivity01 to Avtivity02 after you click a button.
You can go from Avtivity02 to Avtivity03 after you click a button.
-
I want to go from Activity03 to Activity01 DIRECTLY after I click a button.
-
NOTE:
I do NOT want to use Intent, because I want to have Activity01 as if I pressed the back button from Activity02
How to do that, please?
Why can't you use an Intent? You can use the FLAG_ACTIVITY_CLEAR_TOP when you click the button. Edit: If you want to preserve the original instance of the Activity, you can use this flag in conjunction with FLAG_ACTIVITY_SINGLE_TOP.
Do you ever want to be able to press a button from Activity03 to go back to Activity02? If you ALWAYS want it to go back to Activity01, you could alternatively use android:noHistory="true" on Activity02 in the manifest and just call finish() on Activity03.
You can go back more than one screen to which ever screen you need using intent and use flags to prevent going back to same screen :
Intent gotoScreenVar = new Intent(goFromScreenCls.this, goToScreenCls.class);
gotoScreenVar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(gotoScreenVar);
I was had the same problem,
Short answer is add this line to intent.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Long answer is:
I have MainActivity, SecondActivity and ThirdActivity what i want to do is opening SecondActivity from MainActivity and then ThirdActivity from SecondActivity and i want to click a button in the ThirdActivity that close both of SecondActivity and ThirdActivity without calling a new MainActivity i want it to functional as i back to it not creating a new one And the most thing i need is to keep back button functional normal in the ThirdActivity to back to SecondActivity if i need to change some of my choices in the SecondActivity so i can't call finish(); in the SecondActivity after starting ThirdActivity or any other solutions like android:noHistory="true" in the SecondActivity manifest because those solutions kill the SecondActivity and i can't go back to it throw back button.
so this flag solve the problem for me FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_REORDER_TO_FRONT
If set in an Intent passed to Context.startActivity(), this flag will cause the >launched activity to be brought to the front of its task's history stack if it >is already running.
go to reference here
so i tried this code to go back my MainActivity from ThirdActivity when i click the button in ThirdActivity . i hope this will help you.
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
link has good article. This will help you. By the way you can use FLAG_ACTIVITY_REORDER_TO_FRONT to solve your problem.
You can override Back button click on Activity3
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(Activity3.this,Activity1.class);
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
It will work with just one line.
after calling your third activity call finish() to next line. and it will work.
eg.
In your Activity02
Intent thirdActivity = new Intent(this,Activity03.class);
startActivity(thirdActivity);
finish();