How to handle onActivityResult? - android

I am blocking in onActivity result.
Activity A startActivityForResult() to Activity B, Some business reason i am removing activity B and moving to till Activity F. From Activity F have to send setResult() to Activity A same time i have to clear stacks while moving to Activity A.
How to handle this scenario?

We can use a flag from Intent class (didn't try myself) -Intent.FLAG_ACTIVITY_FORWARD_RESULT
Right before calling finish() in each of your middle actvities, you must be making a call to startActivity(intent). Pass this flag to this intent:
showC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
In your last activity, do the regular setResult() ceremony. And then try to get result in your first activity as usual.

using intent start ActivityF and pass result to ActivityA using intent.putExtra
Intent intent = new Intent(ActivityF.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("RESULT", "result to ActivityA");
startActivity(intent);

Related

How to close one activity to another

In my android app i have two activities, such as Activity a and Activity b. I want to close Activity a as well as Activity b from Activity a, I tried the code below, but an exception occurs,
a.this.Finish();//To finish current activity works fine
b.this.Finish();//Exception occurs because i tried to close from a Activity class.
So how to finish Activity b from Activity a? Guide me,
You need to start activity b for result and close it when activity a is closing:
Start of activity b:
Intent it = new Intent(a.this, b.class);
startActivityForResult(it, REQUEST_CODE); // REQUEST_CODE is int value
Finish of activity b:
finishActivity(REQUEST_CODE);
Activity A to B
Intent b = new Intent(A.this,B.class);
startActivity(b);
finish(); // Activity A will close it before starting B Activity.
Activity B to A
Intent a = new Intent(B.this,A.class);
startActivity(a);
finish(); // Activity B will close it before starting A Activity.
To finish() an Activity, you need to get this instance. But I think that if you want to control many Activity, I suggest you to use fragment. I meet your problem before and try to fix them but I meet more issue with that. So let try to use Fragment.

How to kill stack activity

Please tell me any one, how to kill previous stacked activity.My requirement is like that
let I move from activity
A---->B----->C------>D----->E
Then activity stack will be E-D-C-B-A
Now I am at E if my all work is done then I move to activity B
using
startActivity(intent);
this.finish();
this code will be kill only E activity but I want to kill C D activity also.
how can I do that.
thanks.
One solution is to call Activity [B] with flag FLAG_ACTIVITY_CLEAR_TOP
Intent b = new Intent(this,B.class);
b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(b);
this.finish();
This will kill C D E
Intent intent = new Intent(this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
you have to pass activity context into intet so that you can call finish on the activity.
also goo idea would be to start the activities by using startActivityForResult(Intent, int).
Where int is code for the activity you are starting.
later you can call finishActivity(int requestCode) which will kill the activity for the code you provided. You can have codes (id's) for activities in your strings.XML.
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)

Finish() to go back two activities

I have an activity A that calls Activity B for a result.
Activity be calls the camera intent to take a picture then sends it off to a server. On the PostExecute method of the async task of Activity B I call finish() hoping to get back to activity A. However the image I have just received from the intent call of Activity B gets removed. I have to press the back button again to get back to activity A.
How can I skip the middle activity of getting a picture from the camera and return to activity A?
Thanks in advance
Jon
if anyone is interested i used the FLAG_ACTIVITY_CLEAR_TOP
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(i, key);

How to close all child activities from parent in android [duplicate]

I'm new to android. Actually one handler is running in Home Activity A for every 30 sec to check the net connection.
If I'm went to activity C by A->B->C, If there in no net connection at that time, then i want to close Activity B and C, then want to show message box in Activity A.
But My problem is My handler is running for every 30 sec in Home Activity A. But If i was in Activity C or some other Activity how to find which activity is my Application currently focussed now. Then i want to finish those child activities and want to show Home Activity A I have some 9 child activities in Activity B.
I heard about using "FLAG_ACTIVITY_CLEAR_TOP" . I used the code as follows in the handler in Home activity A itself. But got error.
Intent intent = new Intent( ctx, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity( intent );
Here Homepage.class is my Home Activity A and i set that activity in manifest file as
android:launchMode="singleTop"
Please help!
You can start Activity A and close all other activities.
You have to create new intent and add flag FLAG_ACTIVITY_CLEAR_TOP
Intent activityA = new Intent(context, ActivityA.class);
activityA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.StartActivity(activityA);
this will close all activities that are in the stack and are at the top of activity A
When calling Acitivity C, call finish in activity B , when calling Activity A from C , call finish() in activity C !
You can use, According to me two ways,
If you start an activity using startActivityForResult, then you can call finish() in this new Activity when you're done with it and it will return control to the activity that started it.
OR otherwise, May be I am wrong,
Call finish inside onStop 'override method'.
make a uniform resultCode for closing child activities. Eg. you make 911 (should be int) as your resultCode. If you want your Activity to finish and go back directly to parent Activity, you set the resultCode to 911:
setResult(911); finish();
In every child activity, you override the onActivityResult and check if the resultCode is 911. If yes, then call the setResult(911); finish(); until you get back to your parent activity. Hope this helps!
For example, if you want to start intentB, you can do following in old activity:
Intent intentB = new Intent();
intentB.setClass(XYZ.this, abc.class);
intentB.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentB);

How to finish all child activities from parent in android [duplicate]

I'm new to android. Actually one handler is running in Home Activity A for every 30 sec to check the net connection.
If I'm went to activity C by A->B->C, If there in no net connection at that time, then i want to close Activity B and C, then want to show message box in Activity A.
But My problem is My handler is running for every 30 sec in Home Activity A. But If i was in Activity C or some other Activity how to find which activity is my Application currently focussed now. Then i want to finish those child activities and want to show Home Activity A I have some 9 child activities in Activity B.
I heard about using "FLAG_ACTIVITY_CLEAR_TOP" . I used the code as follows in the handler in Home activity A itself. But got error.
Intent intent = new Intent( ctx, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity( intent );
Here Homepage.class is my Home Activity A and i set that activity in manifest file as
android:launchMode="singleTop"
Please help!
You can start Activity A and close all other activities.
You have to create new intent and add flag FLAG_ACTIVITY_CLEAR_TOP
Intent activityA = new Intent(context, ActivityA.class);
activityA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.StartActivity(activityA);
this will close all activities that are in the stack and are at the top of activity A
When calling Acitivity C, call finish in activity B , when calling Activity A from C , call finish() in activity C !
You can use, According to me two ways,
If you start an activity using startActivityForResult, then you can call finish() in this new Activity when you're done with it and it will return control to the activity that started it.
OR otherwise, May be I am wrong,
Call finish inside onStop 'override method'.
make a uniform resultCode for closing child activities. Eg. you make 911 (should be int) as your resultCode. If you want your Activity to finish and go back directly to parent Activity, you set the resultCode to 911:
setResult(911); finish();
In every child activity, you override the onActivityResult and check if the resultCode is 911. If yes, then call the setResult(911); finish(); until you get back to your parent activity. Hope this helps!
For example, if you want to start intentB, you can do following in old activity:
Intent intentB = new Intent();
intentB.setClass(XYZ.this, abc.class);
intentB.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentB);

Categories

Resources