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();
}
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 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.
I have 6 activities - A, B, C, D, E, F.
A opens B, B opens C and so on till F. I am currently at F and then from F, I opened B using Intent.FLAG_ACTIVITY_CLEAR_TOP and maintained A in backstack.
Now I want to send data back to A from B activity when back button is pressed. Please Help!!
there will be two approach one if you use startActivityForResult then need to be used in all 6 activity this will require code change in all activity for onActivityResult this means you have to maintain all 6 activity in stack till your work complted when you complete with F just setresult with data and finish F which will received by E just pass received data with result to D and finish E you have to maintain this till A, this will lead to you extra coding and effort.
Second approach is don't maintain A in stack as you stated opened B using Intent.FLAG_ACTIVITY_CLEAR_TOP and maintained A in backstack. just like this open A with data you have in B, but i don't know this is feasible in your case.
You can use static Method to send data from one activity to another activity (when activities don't have direct link and also don't want to recreate first activity).
In First activty..
public class A extends Activity {
static A INSTANCE;
String data="";
#Override
public void onCreate(Bundle savedInstanceState) {
INSTANCE=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static A getActivityInstance()
{
return INSTANCE;
}
public void getData(String b_data)
{
data = b_data;
}
}
Second Activity:
public class Second extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
}
#Override
public void onBackPressed()
{
A.getActivityInstance().getData("Your data");
super.onBackPressed();
}
Hope this will help you.
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();
}