How to start an activity twice? - android

I have an acitivty A. From there, you can click in the UI and go to activity B that has, among other things, a ListView. In the activity B I have a menu that takes me to activity C. Inside that activity C, I have another menu that can take me to a NEW activity B whose ListView will be populated depending on what I did in activity C.
There is my problem. I can't get the activity B to restart from activity C. The only thing it does is to go back to the PREVIOUS activity B (like the back button have been pushed in activity C).
To sum it up, I want to be able to have the following stack of activities:
A -> B > C -> B
However, I can't get to the second B. Everytime I try to start a new activity B from C, it simply takes me back to the first activity B and the listview is not filled.
Am I missing something?
Code from activity C to B
Intent i = new Intent(thisContext, B.class);
thisContext.startActivity(i);
PS: thisContext here is the context of the activity C, which I assign in the own constructor of the activity C, like this:
public C() {
this.thisContext = this;
}

u can use startActivityForResult() in activity B.... make C as subactivity...
nice example explained here

Related

startActivityForResult usage for 3 activity Android

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

How to detect if a parent activity is in the back stack and can go up without creating a new instance?

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.

Android back button behaviour issue...

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.

Android and popping from Activity C to Activity A or B

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.

How to clear current activities in the stack?

Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.
Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.

Categories

Resources