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.
Related
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.
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.
I have three activities (e.g Activity A, Activity B, Activity C) and Activity A calls Activity B by calling startActivityForResult() method and then, Activity B calls Activity C.
Activity A should receive a return value from Activity C at onActivityResult() method finishing Activities C and B.
For expand question, it may need more activity steps (A -> B -> C -> D -> -> ... -> return to A) to get a result and the concept of question is the first activity should receive a return value from last activity at onActivityResult() method.
Process
Activity A calls -> Activity B that calls -> Activity C
Finish activities (B, C) and remain only the Activity A.
A activity receive a result of C activity at onActivityResult method.
How can I do this?
Apply Scenario
Activity A : is main activity and has a start quiz button and score edit text.
When click the start button, start the activity B.
Activity B : shows round 1 quiz and when finish this, continue to next round.
Activity C : shows round 2 quiz and when finish this, continue to next round.
Activity D : shows round 3 quiz and when finish this, close B, C, D activity and return total score (round1 + round2 + round3) to A activity.
Activity A should be able to recognize the Activity D finished event, so show a grand total score when finished D.
You can call Activity B from A with startActivityForResults. Then do the same for Activity B to start Activity C. Override onActivityResult in Activity B and send that intent to Activity A in this method. Eg:
#Override
public void onActivityResult(int req, int res, Intent result)
{
super.onActivityResult(req, res, result);
setResult(RESULT_CODE,result);
}
Or you can create your own interface in class C and implement it on class A.
Classic scenario to use an event bus in android you can either use an event bus library like otto or greenrobot or Rxjava, OR go with the classical approach of passing data through intents.
Try calling first activity from last activity using intent as below
Intent intent = new Intent(LastActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("RESULT", "result to main activity");
startActivity(intent);
There are three activities: A, B, C;
A, B are normal activities, C is transparent activity to show a kind of guide.
I programmed when I go B from A, C starts automatically on B.
The problem is when I instantly press back button at when B starts,
C shows up on A.
I want to solve this problem, please help.
You could inside activity B
#Override
public void onBackPressed() {
// Here finish activity C
super.onBackPressed();
}
Other solution: Keep a public reference to Activity C
In the manifest.xml set Activity C launchMode = "singleInstance"
Inside Activity C:
public static Activity activity;
onCreate(){
activity = this;
}
Finish it from any activity:
if(ActivityC.activity != null) {
ActivityC.activity.finish();
}
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();
}