I have been using finish() for back button presses to go back to previous activity and its working fine but it is not working as expected for this specific scenario.
Here are a list of my activities and its functions:
Activity A - Show online forum topics
Activity B - Show the comments of a forum topic
Activity C - Post a new comment
After a user post a new comment, he will be directed to activity B.
Issue:
When i click on back button on Activity B, it will go back to Activity C, because it was my previous activity.
Activity A -> Activity B -> Activity C -> Activity B -> Activity C -> Activity B -> Activity A
Expectation
I want the user to go back to activity A from activity B at all cause.
Activity A -> Activity B -> Activity C -> Activity B -> Activity A
I tried using intent to only direct Activity B to A upon back pressed, but it is reloading the data on Activity A which i do not want.
Codes I tried:
case android.R.id.home:
finish();
return true;
//this works if i am only going back and forth one activity
case android.R.id.home:
Intent i = new Intent(ActivityB.this, ActivityA.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("forumLink", forumLink);
i.putExtra("forumTitle", forumTitle);
startActivity(i);
//this refreshes Activity A data
Call finish() on Activity C before going from C to B.
You can make Activity A as parent of B ,and B as parent of C
You have to kill Activity C before going to activity B.
Since your activity c exits, thats why it is going to activity c.
So, use finish() to kill activity c. Killing the activity c will redirect you to activity b.
Related
Hey guys I have 3 activity,
Activity A -> Activity B -> Activity C
This flow I want to perform in my application. I want this scenario in my Activity C
Scenario 1
On Back press from Activity C to open Activity B then again back press then open Activity A
Scenario 2
When something submit on Activity C, I want to open Activity A and I want to hide Activity B
So how can I do this through startActivityForResult. I check through this stackoverlow, but nothing works. Thanks
I'm assuming that the 3 activities are opened in order A -> B -> C, so they are in the backstack in that order. You can check the return values from Activity C, and act accordingly.
In Activity C, setResult(Activity.RESULT_CANCELLED) during onCreate(). That will make it the default return value when activity C is closed (back press or otherwise)`.
Then setResult(Activity.RESULT_OK) whenever something is submitted. After that, you can fininsh() that activity.
Whenever Activity C get closed, in Activity B onActivityResult(), check the return value. If it's RESULT_OK, call finish() (which will return to Activity A). Otherwise, do nothing, so Activity B will stay open
I have an activity A that once the user presses a button it opens activity B.
I do that using:
startActivity(intent)
finish()
The user in Activity B has the option to click on an item and navigate to activity C or press the back button.
Problem:
When pressing the back button, I don't go to Activity A but to its parent.
How can I make sure that on back navigation I go to Activity A, while if the user clicks on an item in Activity B they end up in Activity C?
Suppose you have 4 Activities : A , B , C and D.
User goes from A -> B -> C , i.e. from Activity C user goes to B onBackPress and then A.
But if user goes A -> B -> C -> D, here onBackPress user goes to Activity A.
To implement this you can follow this approach
Start Activity B from Activity A - without calling finish()
Start Activity C from Activity B - without calling finish()
Here you backpress works fine as each Activity is in stack
Start Activity D from Activity C - without calling finish() /or call finish() doesn't matter in this case , as user never goes back to Activity C.
OnBackpress() of Activity D, override the method (onBackPress()), and start a new Activity A with clear the backstack (intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); )
Or for Activity A you make play with launch modes, Make Activity A SingleTask so when you again start Activity A from Activity D, same instance of Activity A will be called clearing all tasks(activites) at top
Just add
startActivity(intent)
Don't call Finish() While redirect to Activity B. It will resolve your back navigation problem.
override onBackPressed() method
#Override
public void onBackPressed() {
super.onBackPressed();
//Set your intent
Intent intent = new Intent(currentAct.class,Activity A);
startActivity(intent);
}
I have 5 activities (A, B, C and D).
A -> B -> C -> D
On activity B and C, if we press the return button, I would like the previous activity to be called (respectively A and B).
Activity D is an end activity without a return button.
When I use "finish ()", it's currently going back to Activity C.
I would like to go back directly to activity A when we reach activity D by executing finish() without going through B and C.
However since B I must always be able to return to A and too C can back to B.
"A" is an activity in launchMode singleTop.
android: noHistory = true on activity D does not allow me to return directly to A.
The only solution I have found for the moment is to use startActicity (A) instead of "finish ()" but I find that this solution is not a good pararique.
Do you have a solution to my problem?
In your D activity use this:
Intent i = new Intent(this, YourFirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
This way, you'll return exactly to your A activity.
I have many activities.
A is list activity.
B is form activity. And generated dynamically. I open this activity two time in a row.
C is result activity.
A -> B -> B like simple push new activity.
if result is success I want to clear all forms when I push C.
A -> B -> B -> C ==> A -> C.
if result is failed when Im in C activity, it can be return different activities like above.
A -> B or A -> B -> B
I use cleartop when I push C but it clear all activities how can I save A activity's state.
How can I manage activities like fragments.
* When I back from second B, first B should open *
You can achieve it by following below steps:
Set android:launchMode="singleTask" of ActivityA in AndroidManifest.xml file.
Set onNewIntent method in ActivityA like below:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle mBundle = intent.getExtras();
if(mBundle!=null){
String launchActivity = mBundle.getString("activityName");
switch (launchActivity){
case "ActivityD": // This is Activity Name Here it is ActivityD.class
startActivity(new Intent(ActivityA.this, ActivityD.class));
break;
}
}
}
Now Start ActivityA from ActivityC like below.
startActivity(new Intent(ActivityC.this, ActivityA.class).putExtra("activityName", ActivityD.class.getSimpleName()));
It will call onNewIntent method of ActivityA and will match the argument and launch ActivityD from ActivityA. So your ActivityA will remain in stack and ActivityD will be added in stack on Top.
Now to achieve A -> B from A -> D you can call finish() method in ActivityD when you start ActivityB from ActivityD.
Regarding ActivityLaunchMode please refer this link
Hope it will work for you!
On Activity A you cant goto Activity B using this
startActivity(new Intent(Activity_A.this, Activity_B.classs));
From B to C
startActivity(new Intent(Activity_B.this, Activity_C.classs));
finish();
From C to D
startActivity(new Intent(Activity_C.this, Activity_D.classs));
finish();
From D to A
finish();
It will close Activity_D and Resume() Activity_A
When you startActivity from Activity B to Activity C or C to D you need to call finish(); expect for Activity A.
Like
A -> D.
Start Activity(new Intent(A.this,D.class));
Now, when you click Activity A --> B then B--> C and then C--> then you should do like
A -> B.
Start Activity(new Intent(A.this,B.class));
B --> C
Intent intent =new Intent(B.this,C.class);
startActivity(intent);
finish();
C --> D
Intent intent =new Intent(C.this,D.class);
startActivity(intent);
finish();
Now, When you press back you will be return to activity A. You should remove all clearActivityTop(); from code.
You can achieve that by simply using fragments more often
Activity:: When an activity is placed to the backstack of activities the user can navigate back to the previous activity by just
pressing the back button.
Activity can exist independently.
Fragment:: When an fragment is placed to the activity we have to request the instance to be saved by calling addToBackstack() during
the fragment transaction.
Fragment has to live inside the activity
source
By using fragment you can easily remove any page you want by using tags, however if you still want to use activity
if you use startActivityForResult() instead of startActivity(), then
depending on the return value from your activity, you can then
immediately finish() the predecessor activity to simulate the
behaviour you want. By using this method in all your activities, you
can have this behaviour cascading the activity stack to allow you to
go from activity D to activity A.
In my android activity I have 3 activity A B C
But when i start A - >B - >C - >A and when i click back all activity remain A-C-B-A
I want when I press back in current activity it returns to activity A
Help
When you want an Activity to not be added to the back stack after leaving it just call:
finish();
So for example, to start Activity B from Activity A and get rid of Activity A completely:
startActivity(new Intent(ActivityA.this, ActivityB.class));
finish();
Now if you press the back button in Activity B, it will exit the app because there are no other Activities in the back stack.
For your specific situation, you can call finish on everything except Activity A, which will make Activity A the only Activity in your back stack. So the back button will always send you back to Activity A no matter what Activity you press it from.