Do not kill the activity on back key pressed - android

I have two Activity A and B.
Launched Activity A.
Started Activity A ---> Activity B
On Backey pressed Activity B is destroying and navigating to A.
I do not want to destroy the Activity B, maintain in stack to reuse the same activity when i navigate from A to B again.
Actual:
Activity B is loading URL in webview oncreate which creating every time and reloads the URL.
Expected:
It should start Activity A to Activity B by not creating and should not reload again.

Try this solution
#Override
public void onBackPressed() {
this.startActivity(new Intent(YourActivity.this,ActivityA.class));
}
Override the onBackPressed() in ActivityB and then launch the activity A

#Override
public void onBackPressed() {
moveTaskToBack(true);
}
It'll move your activity to Stack and it'll remain there when you'll return.
For more details look here

From Activity B, below code will not destroy the Activity B and navigate to A
#Override
public void onBackPressed() {
this.startActivity(new Intent(ActivityB.this,ActivityA.class));
}
Navigating from Activity A to B, set below flag to intent which will not recreate the activity.
setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
Thanks.

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.

How to avoid duplicate activities in back stack android?

I have an activity A, B, C, activity A opens activity B (A -> B) and activity B opens activity C now the back stack will be (A -> B -> C) Now Activity C opens activity B (A -> B -> C -> B) and at some condition the activity C will be destroyed in that case (A -> B -> B) is the back stack. when i click back in Activity B then the app again goes to activity B only which i don't want to do. i need to go back directly to activity A. I tried using single top but the single top works only while the activity getting created not something happens in the back stack. Any way can we achieve this ?
You can try this:
override onBackPressed method on activity B
#Override
public void onBackPressed() {
Intent i = new Intent(B.class, A.class);
i.startActivity(i);
}
add this code to each of your activities.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
this code will finish your activity from stack when you move back to the last activity.

How to go previous Activity by using back button

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

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