Going Two Activities back? - android

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();

Related

Back button from activity 2 closes down the app instead of going to activity 1 in Android 7

Pressing back button from the second activity returned to the first activity before without problems. I then updated to Android 7.
Then the whole app closed when pressing back button from the second activity.
I know that there are threads about this here and I have checked them all. Basically, they say that finish() should be avoided from the first activity.
I don't call finish(), so that is the problem here. It is difficult to solve, because it works like it should when I launch the app from Android studio.
It returns to the first activity from second. The problem occurs when the app is started by pressing its icon (not from Android studio).
Pressing back from the second activity closes down the whole app. How can I solve this? Here is some of my code:
Activity 1:
Intent glIntent = new Intent("astral.worldstriall.GLActivity");
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
The below code should be used in 2nd activity so that when u press back button it terminates the current activity(2nd activity) and goes back to previous activity
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
I think you just misused the Intent constructor. According to the documentation, you used this constructor Intent(String action). The one that you atually want should be this one Intent(Context packageContext, Class<?> cls).
In the first activity (therefore this being the instance of your first activity), you should write:
Intent glIntent = new Intent(this, astral.worldstriall.GLActivity.class);
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(glIntent);
For going first to second activity...
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
Use below code for going in previous activity...
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i=new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
}

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

Going Back to MainActivity from Child Activity in Android

In My Android app the flow looks like this: Main Page -> Page1 -> page2 -> page3. In Page1, Page2 & Page3 am having Home button, when clicked on Home button, It should launch Main Page(MainActivity). Here MainActivity Class gets the JSON Data from the server.
Now my question is: when am in Page1, Page2 Or Page3, if i click on Home button it should go to MainACtivity.At the same time, When Am in Page2, if i click device back button it should go to Page1. Simillarly, If am on page3, If i click on device back button it should go to page2.
For this, The usual solution i have used like,
//In Page1 Activity, Under Home button OnClickListner:
Intent in = new Intent(Page1.this,MainActivity.Class);
StartActivity(in);
With this, Everytime the MainActivity Class gets load & Communicates with Server, that results low performance in app.
Another Approach i tried i.e,
//In Page1 Activity, Under Home button OnClickListner
finish();
This works fine for page1. If i use the finish() in the Page2, it launches the page1 activity. Alternatively i have used somewhat like this:
//In Page1 Activity:
Intent in = new Intent(Page1.this,Page2.Class);
startActivity(in);
finish();
And, in Page2, under Home Button OnClickListener i have written finish(). With this, when am in Page2 if i click on Home button, it will launch the MainActivity. At the same time if i click back button of device, it is launching MainActivity. It has to go to Page1.
Please, Can someone suggest me what i need to use for this suitation.
Thank you All.
You should set a flag in your intent in the child activities.
Intent i = new Intent(Page3.this, MainActivity.class);
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
http://developer.android.com/guide/components/tasks-and-back-stack.html, for more information.
Use This code:
in the OnClick of Home button in all the page, write this:
Intent in = new Intent(Page1.this,MainActivity.Class);
startActivity(in);
finish();
in onBackPressed of page1, write:
Intent in = new Intent(Page1.this,MainActivity.Class);
startActivity(in);
finish();
in onBackPressed of page2, write:
Intent in = new Intent(Page2.this,Page1.Class);
startActivity(in);
finish();
in onBackPressed of page3, write:
Intent in = new Intent(Page3.this,Page2.Class);
startActivity(in);
finish();
Try adding a flag to your intent before tarting the activity like this.
Intent mIntent = new Intent(Present_Activity.this, Next_Activity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
EDITED
And in your main Activity before you call finish make a call to method:
moveTaskToBack(true);
Somehow i got the solution to my suitation..
On Page2, Home button SetonClickListener have written code like this:
Intent in = new Intent(Page2.this,MainActivity.class);
startActivity(in);
Under Manifest file:
For Main Activity have used like this
android:launchMode="singleTask"
with this, Everytime click on Home button launches the instance MainActivity but doesnt communicate with servers.
Have a look at docs , you can use android:parentActivityName attribute in manifest file while declaring any activity.
Example:
<activity
android:name="com.example.Page2"
android:parentActivityName="com.example.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
You don't need to start MainActivity using Intent.It will handle to call ParentActivity while executing onBackPressed.
If you need to add SupportLibrary , you can download it from here.
EDIT :
you should manage Activity Lifecycle as mentioned here.
Inside MainActivity's onCreate method, initiate execution of AsyncTask to load data from server.
and when you move back from Page2 , onResume of MainActivity will be called. so your data will not be loaded from Server again.
I hope it will be helpful !!

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);
}

How to dismiss previous activities in android at a certain point?

I have activities A -> B -> C where A and B are for login and verification purpose. When a user reaches activity C, A and B are not necessary any more. That means if a user press BACK in C, the application should not go thru A and B, instead it should go back to HOME screen.
What's the conventional way to implement this in android? Thanks.
EDIT: to clarify a bit, user should be able to go back to A from B during login/verification phase, but not from C to A or B once the user reaches C.
When going to next activity call
finish();
before starting next activity.
Or, if not pressing back but going to next activity:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent data = new Intent(this, HomeScreenActivity.class);
data.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(data);
finish();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
This way you override the back key behaviour and start a selected activity removing all other activities in between from the stack.
The simplest is to add
android:nohistory="true"
in your manifest for Activity A and B.
Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

Categories

Resources