android insert into activity stack - android

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

Related

Do not kill the activity on back key pressed

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.

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

Using Intents to navigate between activities

I usually navigate between the Activities using Intents
Pictorial representation looks as below::
In Activity - A .......... There is a next button to go to activity B
Photos.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent PhotoIntent=new Intent(a.this,b.class);
startActivity(PhotoIntent);
}
});
In Activity - B .......... There is a next button to go to again activity C
Map.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent PhotoIntent=new Intent(b.this,c.class);
startActivity(PhotoIntent);
}
});
Now in Activity C .... there is only one back button
If I use it i similarly to above code I can't go to two activities
depending on the navigation i came from
Suppose i came from A to C ------ when i use Back button in C go to A
Again when came from B to C ----- when i use Back button in C go to B
There is only one back button in Activity C
[Edit]
Its like Say i came from Activity A to Activity C ----- When i USE the only back button in Activity C it should go to activity A
&
For the same scenario if i came from Activity B to Activity C - - - - When i USE the only back button in Activity C it should go to activity B
------- I am trying to relate a switching case kind of mechanism for Back button in ActivityC
How to Achieve this
Hope i am clear
There is only one back button in Activity C
If I understand correctly, Activity C can be started from both Activity A and Activity B. If this is the case, the back button in Activity C should have the following code:
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Takes you to whichever activity launched C
finish();
}
});
When you launch Activity C from Activity A, the stack is .... {Activity A}, {Activity C}. Calling Activity#finish() in Activity C will take you to Activity A. Same scenario unfolds when Activity C is started from Activity B.
Note that if you call finish() in Activity A after launching Activity C, the back button will take you to the Activity that is positioned before Activity A in the stack.
Do like this
backBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
In this way , you have called back button functionality of device Now the OS will decide where to navigate according to the Stack, whether Activity A is present or Activity B in the stack.
call finish() or onBackPressed() function on back button click event.

Categories

Resources