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
Related
I have a system which has several Activity, Activity X, Activity A, Activity B, and Activity Y. Activity X consist of list of A. If we click on A, it will go to Activity A. Activity A consist of list of B. If we click on B, it will go to Activity B. Activity B will also consist of list of B. So, if we click B on Activity B, it will go to Activity B. It is possible to end up the stack like this: X -> A -> B -> B -> B -> B -> B. According to proper navigation by Android, if we click Up on Activity B, it should go to Activity A, no matter how deep the stack is. So, every B on the previous stack should end up on A. Up until now, it's simple. I just need to set the parent class of Activity B as Activity A.
The problem is I can go to Activity B from Activity Y. If I open the Activity B from Activity Y, this Activity B is not always know how to open Activity A because the parent of B can be another B or A. B only know his parent. The problem is how to detect that Activity A is reachable automatically without creating a new instance from Activity B? This way, I can make my code like the following on Activity B.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (canGoUpToActivityA()) { // this means this Activity B is opened from Activity A directly (A -> B) or indirectly (A -> B -> ... -> B), not from Activity Y
NavUtils.navigateUpFromSameTask(this);
return true;
} else {
Intent intent;
if (TYPE_B.equals(mParent.getType())) {
intent = new Intent(this, ActivityB.class);
} else {
intent = new Intent(this, ActivityA.class);
}
intent.putExtra(EXTRA_ITEM, mParent)
startActivity(intent);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
EDIT
If anybody down voting me because they think the solution is as simple as singleTop to Activity A, that's mean you guys don't understand my problem. See the last sentence of my first paragraph. I don't have any problem with going up from Activity B to Activity A, no matter how deep it is if Activity B is opened from Activity A.
Without the above onOptionsItemSelected, I can go up to Activity A from Activity B if Activity B is opened from Activity A directly (A -> B) or indirectly (A -> B -> ... -> B). This is not my problem.
The problem comes when I opened Activity B from Activity Y. I can't just go back to Activity A because Activity A rely on information from Activity X. If I open Activity B from Activity Y, then I go up, I should go to the parent of Activity B, where it can be A or another B.
The above code without canGoUpToActivityA() part will solve my problem. With that code, when I open Activity B from Activity Y, then going up should always go to the parent of B. That's already correct (B only know its parent, which can be not A).
But, when I open Activity B from Activity A, then I go up, it will launch the parent of Activity B. If I open Activity B from Activity A directly (A -> B), it's is indeed what I want. But, when I open Activity B from another Activity B (A-> B -> ... -> B), that's the problem. Because I should go up to Activity A, not the parent of Activity B which can be another Activity B.
I'm still not clear on what exactly you want, so I'll break this up into snippets:
If you want to launch a parent of Activity B and ensure that it isn't "launching itself", you can use the singleTop flag as noted by others to ensure that only one instance of B is ever on the top. You mentioned you don't have an issue with that, but you've listed that as an example, so just keep that in mind.
You've also mentioned that you check if the parent is A before launching Activity A. If all you want to do is actually launch the previous activity in the stack, there is no need for an Intent. Just finish your activity and it will exit the stack, showing the previous activity.
If you have a set activity that you should launch when going back, you can simply launch it with an intent. At this point, it doesn't matter what the previous activity is, because you are explicitly starting an activity for a given class. Again, you may look at the FLAG_ACTIVITY_REORDER_TO_FRONT flag to ensure that an existing activity is not recreated.
Lastly, if you have some complex logic that requires you to know the full stack history, you can always pass data through your bundle. A simple example is an arraylist of the simple class name so you know everything in the current task.
Edit: Since you've also added that you depend on information from previous classes, I take it that you need more than just the class name.
Note that you can always pass data through the bundles so that they are retained through all subsequent activities. You can also pass all the current data by calling putExtras.
And if your entire flow is geared towards going to a child and then passing data back to a parent, consider using [startActivityForResult](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)). That way, you can set result codes and data and finish your activity to directly pass information to the previous activity, without launching a new intent in a forward flow manner.
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.
I'd like to kill all activities when the user hits the back button and returns to my main activity A. My App has the following 3 activities:
A -> Displays users selections before they hit search and creates activity B.
B -> Displays a list of urls's based on the users selection and creates activity C.
C -> Opens a Webview and displays the page selected by the user.
Currently, i don't call finish() after starting activity B so when the user hits the back button in activity C they can return to the list of url's and make another selection if they want.
I'd like to create a new activity A (without the users initial selections) and ensure the existing activities B and C are killed if the user hits the back button in activity B?
Any help would be appreciated.
Thanks
O.
You can return to activity A by overriding the back button handler in activity C and launching activity A with the flag FLAG_ACTIVITY_CLEAR_TOP set, or alternatively (perhaps preferably in your case), you can just set noHistory for your activity B.
You'll have to reset activity A manually in some way in this case though.
I have an application that have three activities, lets call they A, B and C for convention.
A calls B with StartActivity.
When user hit the back/cancel button, I have to call Activity C, so I implemented in OnPause of Activity B to call Activity C and I need return from activity C, so I called Activity C with startActivityForResult and implemented the method onActivityResult in Activity B to get the return.
Everything is working fine, but when activity C finishes, the application is getting back to Activity A, and I need Activity B.
I have to call Activity B explicitly again or I made something wrong?
I'm really not sure what you want to achieve with this behaviour. Anyway, you get back from C to A, because you pressed the Back key, and didn't override it's behaviour in onBackPressed(). So the current Activity (B) just got finished, hence onPause() was called, so C started. But by the time C becomes active, you'll only have A and C on the Activity stack.
You need to override onBackPressed() in Activity B, and call C from there, forget onPause().
You shouldn't call another activity on hitting back button. Back button will pop activity(B) out of the stack. When you clicked back from Activity B Android will finish that activity and kill it. This is a standard workflow which better do not mess up. Place some button in activity B and call C for result from there, then you will be able to get a result in B activity.
When you get to the onPause() in activity B, it is already shutting down, so when you return to it, it will be gone.
You can either, as you say, start B from C when done, or start C in B's onBackPressed() (and then return from that method without calling the super method). This overrides the default action to shut down the Activity.
I have three activities in my activity stack, A (main) -> B -> C.
A starts B starts C.
My C is a dialog box with one button that will take me to A and one button that will take me to B. The Activity B must therefore have history, so if C calls finish() I will always end up in B.
Given this, how do I (efficiently) set this up? i.e. how do I (efficiently) go from C to A?
I assume that you want to finish() B if the user selects the A button on C. Have B start C for a result (startActivityForResult()) and have C send back to B the result. If the result is "go to A", then B can just finish().
One way is to set up the onActivityResult method in activity B.
Based on the button clicked, return from activity C with different return codes, which you can access in the onActivityResult method of activity B. Based on the return code, you can either stay on the activity B, or close the activity which will take you to activity A.