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.
Related
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.
Let's say I got a fragment page called "A" and "B" and an activity called "C".
When I click the add button in Fragment B, it will takes the user to activity C. When the user input all the information in activity B and press submit, it will take the user back to fragment B and reload it.
My situation is I use the finish() function on "C" while I press the submit but when it go back to the fragment B it doesn't refresh.
My code:
private View.OnClickListener mSubmitButtonOnClick = new View.OnClickListener(){
#Override
public void onClick(View v)
{
if(getArray() != null) {
insertRecord(getArray());
finish();
}
else{
Toast.makeText(AddClaimFormPage.this, "Please select at least one item to claim.", Toast.LENGTH_SHORT).show();
}
}
};
Please help, how can I reload the fragment B when I used finish()?
You need to override the onResume() on fragment B:
#Override
public void onResume()
{
super.onResume();
// Load data and do stuff
}
Refer Fragment Lifecycle for more details.
you have to manage stake of fragment and when you back from another activity to previous activity than load fregment which is the top on stack.
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();
}
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();
}