How to go previous Activity by using back button - android

I have created two activity i.e Activity A & Activity B ,if i clicked Next Button i.e in Activity A going to Activity B properly But when i click on back button i want to go from Activity B to Activity A and page swipe from left side to right side and on click next page swipe right to left,
here is my code
public void onBackPressed() {
Intent intent = new Intent(ActivityB.this, Activity.class);
startActivity(intent);
finish();

Just Remove finish() from Activity.
Because when you go to second activity and finish first activity, there is no any activity and stack.
So If you click back button from second activity, application will be finish if there is no Activity in stack.
You should use this Approach.
Ex.
In Activity.java
Intent first = new Intent(Activity.this,ActivityB.class);
startAcivity(first);
// Don't use finish() here.
In ActivityB.Java
Just click on built in back button.
or If you want to use your own back button.
Use finish(); in button click event.

You can use only onBackPressed();
public void onBackPressed() {
super.onBackPressed();
}
Android Overriding onBackPressed()
How to go previous Activity by using back button

No need to put an intent and start a new activity that would take you to previous activity.
Just call 'finish()'
It would go back to previous activity as Android activities are stored in the activity stack
If you have other activities that are present in between the activites say if android stack is filled with Activity A>Activity C>Activity B,If you want to go to Activity A on finish of Activityy B then you have to set an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP

Just use finish() no need for intent as A is already in stack and when you finish B, A will come to surface
public void onBackPressed() {
finish();
}
Read this to learn more about android activity stack.

public void onBackPressed() {
// TODO Auto-generated method stub
finish();
super.onBackPressed();
}

Related

Clear upto a specific activity from the stack history

Suppose I have activities named A, B, C, D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C.
Now I have a button named "Success" in the activity D. If suppose, I press the button "Success" in the activity D, then the activity C should be removed from the history stack and go to activity B, but on Pressing back key from the activity D should display activity C instead of B and Clear D from stack. Please can anyone help me to resolve this problem?
Add this Code in your Activity_D for Button click
btnSuccess.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});
Also add this in your Activity_D to Handle Back Button Pressed.
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
From Activity_C to Activity_D
startActivity(new Intent(Activity_C.this, Activity_D.class));
Don't call finish() here.
When you click on Success button, you can use the following line of code to open your B activity:
Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();
Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call
finish();
Hope it will help you out.

Android: How to go back previous Activity

I have four activities
ListActivity
Itemdetail_Activity
record1_Act
record2_act.
I can go from ListActivity -> record1_Act and from ListActivity <-record1_Act properly but when I want to go from Itemdetail_Activity -> record2_act and return back from Itemdetail_Activity <- record2_act it always goes record2_act to ListActivity.
I even used i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); but it does not work. How to solve this issues?
Here is my code
Intent i = new Intent(record2_act.this, Itemdetail_Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Intent i = new Intent(record1_Act.this, ListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
After starting activity remove finish() call because it will clear that activity from stack and i see you have used finish in your code. also you do not need to set these flags.
In activity 4, implement onBackPressed() as,
#Override
public void onBackPressed()
{
Intent i = new Intent(4.this, 3.class);
startActivity(i);
}
call this method if you have any back button. And if you want to link the backpress for every activity you need to implement onBackPressed() method in every activity.
you can call onBackPressed() if previous activity not finished also you can use :
this.finish();
// Or
Activity currentActivity = this;
currentActivity.finish();
and if previous you finished activity you have to track user in activities and make list of activities, then move to :
activitiList.get(activityList.size() - 2);
// activityList.size() - 1 is last activity and activityList.size() - 2 is previous activity
Do check if ItemDetailsActivity is NOT calling finish()

How to implement the following?

Given:
6 activities. (A, B, C, D, E, F)
Each activity consists of several edittexts or a camera implementation and a next button to goto next activity (The activity which is going to be opened depends on the value entered by the user).
If edittexts are displayable or not comes from the server.
Incase there are no edittexts on the layout then only next button is shown.
Flow of the application: A->B->C(->D)->E->F
The Activity D opens only when a certain condition is met in activity C.
Todo:
Incase, The activity contains no edittext and only next button i should be able to skip this activity.
If the flow of my application is like: A->B->C->E->F Then when i press hard back the flow should be F->E->C->B->A
If the flow of my application is like: A->B->C->D->E->F Then when i press hard back the flow should be F->E->C->B->A
If the flow of my application is like: A->C->D->E->F Here we skipped B because there are no views in Activity B, When i press hard back the flow should be F->E->C->A
what i did:
To skip the activity when no fields are available inside onCreate(), I am checking if any field is displayable if no then i add the activity name in a stack if it is not present in it and then open the next activity.
// Block for skipping this screen
if (skipScreen) {
Intent i = new Intent(B.this, C.class);
startActivity(i);
finish();
} else {
if (!Constants.st.contains(B.class)) {
Constants.st.push(B.class);
}
}
And when i press back from an activity i pop() the name of that activity from the stack and peek() at the top of the stack and jump to that activity.
public void onBackPressed() {
super.onBackPressed();
Constants.st.pop();
Intent i = null;
if (Constants.st.isEmpty()) {
i = new Intent(B.this, A.class);
} else {
Class<Activity> jumpTo = Constants.st.peek();
i = new Intent(B.this, jumpTo);
}
startActivity(i);
finish();
}
I think the statePattern will be very helpfull. Here is a short tutorial.

Navigate to activity by back button doesn't call onstart()

I have an activity(A) and I need to set some text after activity becomes visible to user ,first time I navigate to activity everything is OK,but when I navigate from (A) to activity (B) and press back button ,it returns to (A) button doesn't call onstart of (A).what is the problem?
Back button is navigation to previous activity in activity stack, which is already created and thus its onResume method will be called. So you can do what you want inside onResume().
If it is must for your activity to create new instance do as follows:
If you are on activity A and going B, call A.finish() so it discards A from activity stack, and on B override backPressed and create a new instance of A.
#Override
public void onBackPressed() {
Intent i= new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.finish();
startActivity(i);
}

android insert into activity stack

Here is the question:
Let's say the activity stack consist of A->B->C.
If user followed the order eg: Start A -> B -> C, pressing back button will cause C->B->A.
However, if user entered directly into activity C (eg: via notification), pressing back button will cause the app to close, instead of going into B->A.
How do I insert the into the activity stack to become A->B->C, so that when user pressed back at C, it will always back to B.
Thanks
just overide the onBackPressed() method and startactivity B in activityc and startactivity a in activity b.
in activty c have these code::
public void onBackPressed(){
startActivity(new Intent(this,ActivityB.class));
finish();
}
and in activity b have these code::
public void onBackPressed(){
startActivity(new Intent(this,ActivityA.class));
finish();
}
and in activity a have these code::
public void onBackPressed(){
finish();
}

Categories

Resources