How can I manage activities like fragments stack - android

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.

Related

Return on activity A since end activity D with keep back on activity B ans C

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.

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 Backbutton Pressed Return to Two Activities

This may sounds simple, but I am so bewildered by it. I have searched but not found any solution.
My question is: How to return to two activities when back button pressed?
Like this: let me say that I have activities A, B and C (A -> B -> C). What I want to achieve is when I am on activity C and press the back button, It should return me to activity A. When I am on B and press back, it should return me to A too.
It may be implemented into a project with many activities, so I assume that I don't need to set the class name of where to return, It should be recorded automatically by the android. How to achieve this?
Thank you
A possible solution is calling startActivityForResult() from Activity B, so that on the callback of the created Activity C, the previous Activity B gets finished as well. The callback function is called onActivityResult(), and in that, you want to call finish().
Example:
ActivityB:
Intent i = new Intent(this, ActivityC.class);
startActivityForResult(i, 0);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
this.finish();
}
ActivityC:
//do stuff
This way, when you press Back (or call finish()) in ActivityC, it will call back on ActivityB's onActivityResult() function, and it will end them both.
You can override the onBackPressed method and sent an intent to the activity you want.
#Override
public void onBackPressed()
{
// code here to send intent to the activity A
}
One thing, I'm not sure if this goes well with the activity stack but you can alway try.
you can finish activity B , when you are starting intent for activity C, then activity stack will have activity A, and when you press back on activity C, activity A will be displayed.
just override onBackPressed in Activity C, and finish() it.
You can call the Activity that you want to go back to with a special flag (FLAG_ACTIVITY_CLEAR_TOP) from your onBackButtonPressed which will skip/remove the other activities in between. This way you can go back from C to A.
See: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
I think this is better than messing around with the finish() or starting activity for result when there is no result to return.
A --> B --> C
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will launch a new intent for Activity A and clears all the other activities from Stack.

return back and skip one activity android onbackpressed

Here is my case
assuming that I have those activities
A -> B -> C -> D
I want to come back to A when the user click on the back button when he is in D
but also I want to come back to B when the user click on the back button of C
I thought to call finish when I go to C from B, but in this case I can't return to B if I click on back of C
how can I fix this issue without calling onBackPressed on two activities?
thanks
When you start each activity, you will need to use
startActivityForResult()
And then have a system of flags saying who is being stopped which are passed back. So, activity D:
timeToEnd() {
setResult(RESULT_D_CLOSING);
finish();
}
And then in A, C, D
onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode)
case RESULT_D_CLOSING:
// Close on upwards
setResult(RESULT_D_CLOSING);
finish();
case ....
// You get the idea
}
}
Have you tried using FLAG_ACTIVITY_CLEAR_TOP flag in your intent (used to start "A" from "D")?
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
You have 2 options here. You can either start the activities with startActivityForResult and then onBackPressed of each activity pass a flag back that tells the activity to either exit of stay open.
An other option would be to implement A,B,C,D as fragments in the same activity put all the fragments on the backstack as you open them and then onBackPress you can programmatically loop through the backstack to see what you want to do.

Android finish parent Activity

I have 3 different activities in a TabGroupActivity. Let's say A - Parent, B - Child 1, C - Child 2.
A --- starts --> B
B --- starts --> C
and I have an alert dialog in C which shows some message. I want to go back to activity A when I press Ok button on dialog.
But the problem is that I can open activity C from other activities too and I want to go back to their parent activities too. So I need to make something which will work no matter which activity opens C. I've tries with this one but didn't work :
Intent intent = new Intent(Synchronization.this,Synchronization.this.getParent().getClass());
but it didn't help me. Any suggestions?
You just have to make use of two Activity methods viz. startActivityForResult() and onActivityResult()
Example : http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents
Here goes the logic :
In ActivityB
Start ActivityC by using startActivityForResult(activityCIntent,INT_CODE);
In ActivityC
Now check if Dialog's OK Button is pressed, if yes then set the result using setResult(RESULT_OK,intent); and then call finish();
Then control will be redirected to ActivityB's onActivityResult() method.
Now inside onActivityMethod() check whether result_code==RESULT_OK and requestCode = INT_CODE. If yes then simply call finish();
In the Activity B start the C Activity as startActivityForResult() so when you finish the C activity it will back to the B with result. As the result you may pass flag with the intent object.
Now when you finish the C activity with ok button then set the result as RESULT_OK into the setResult() if you need to pass the data back to the B activity you may set the data into the Intent add this intent with the setResult() method and then finish the C Activity.
Now in B check the requestcode is from the C then finish this Activity. As you start this C Activity you can also start the B Activity for the A Activity.
And you need to override onActivityResult() in B activity and if you start the B Activity as for result then also you need to define also into A Activity

Categories

Resources