Android Backbutton Pressed Return to Two Activities - android

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.

Related

How can I manage activities like fragments stack

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.

Getting a Result from an Activity

To describe the situation, say that I have 3 activities: A, B, and C
and there is a button in activity A which starts activity B, and there is a button in activity C when it is clicked, it should send a result from activity C to activity A
My Question is...Is there a way to pass a result from C to A? If there is a way, what it is?
Note: It would be good if the way you give uses the methods startActivityForResult(...) and onActivityResult(...)
Thank you in advance
If you just using simple types like String objects you can use Bundle and supplementary variables in B,C activities. And transfer it from C->B->A using onActivity result. Or you could use Shared preferences.
There's a flag of Intent called FLAG_ACTIVITY_FORWARD_RESULT. Call:
Intent intent = new Intent(this, ActivityB.class);
startActiivtyForResult(intent);
when starting Activity B (by calling startActivityForResult(intent)).
When opening C, call:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActiivtyForResult(intent);
finish();
which means: open C, close B. Then, when closing Acitivity C, call:
setResult(123);
finish();
the result from C will go back to A.
You can use Intent to transfer data if you are navigating from Activity C to Activity A. Otherwise, I would suggest using an Interface, and then pass the data as parameter in a callback method present in Activity A. You said you wanted answers like onActivityResult, for that I guess the Activity C should exit or something to invoke onActivityResult in the Activity A

Android-finish function in activity didn't work

I extend activity A, B, C from MainActivity.
A start in first and A start B and B start C.
when I use finish() in onclick special Button in activity C, Activity A is start, but I want activity B start.
What I do for this case?
Start all activity like below:
Intent i = new Intent(this,NewActivity.class);
startActivity(i);
don't add finish() method, then inside button click just call finish() method. It will bring you back to previous activity.
You can't reliably expect Activity B to be on the stack when Activity C finishes since Android can destroy a background Activity if it needs memory. If your use case suggests that Activity B should always become visible when Activity C is closed, you should start it explicitly, as described in Navigating Up to Parent Activity. In your case, I think it's appropriate to use NavUtils.navigateUpFromSameTask(this) in onBackPressed().
AActivity.java
Intent i = new Intent(this,BActivity.class);
startActivity(i);
BActivity.java
Intent i = new Intent(this,CActivity.class);
startActivity(i);
CActivity.java
Intent i = new Intent(this,BActivity.class);
startActivity(i);
finish();

How do you bring back a destroyed activity?

I have an application that starts activity A. The user can then start service S from activity A. When a certain event happens, service S starts activity B. Activity B only has one button, and when pushed, should return to activity A. Everything works fine, except for when activity A was closed out using the back key. When the back key is pressed, instead of onPause, onDestroy is called. So when activity B is dismissed, I get sent back to the homepage instead of Activity A. So my question is, how can I make sure that Activity B reopens activity A if activity A is onPause, and also reopens the activity A when it's onDestroy has been called. I'm thinking to use intent filters, but I can't figure out to get the right combination. I do not want multiple instances of Activity A. Thanks for the help.
on activity A:
public void onBackPressed() {
moveTaskToBack(true);
}
or on Activity B, force it to start activity A,
public void onBackPressed() {
Intent i = new Intent(B.this, A.class);
startactivity(i);
}
but since you implement the first one,
you dont need this last code.
I Hope it helps
Since you don't need to save A state,
you can override onBackPressed() method in B to start A again.
And if you don't want multiple instances of A, use FLAG_ACTIVITY_CLEAR_TOP when you start B.
Hope this helps

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.

Categories

Resources