I've start activity using startActivityForResult. Inside started activity I want to finish calling activity using finishActivityFromChild. However it doesn't work. May be there is other method for this.
Is there a reason for killing the parent activity inside the child activity? Did you just want to kill the parent immediately after starting the child? I'm not seeing the point of using startActivityForResult() if the parent activity isn't using a result returned from the child activity, and is just getting killed by the child activity.
If you simply don't need the parent activity to exist after starting a new activity, you can use the following code:
Intent intent = new Intent(this, MyNextActivity.class);
startActivity(intent);
finish();
If you wanted to actually return a result to the parent activity from the child activity, and then finish the parent activity, you would have to return to the parent activity by finishing the child activity in order for onActivityResult() inside the parent activity to be run. If the child activity needs to return a result for processing something it cannot do by itself, you may need a Service to handle it instead.
If you startActivityForResult then what would the result go to if you killed the parent activity? It might be easier to simply lock out the back-button in your child activity, so that the user would have to start the app over to get back to it. There's probably some (very unrecommended) way to get a reference to your parent activity and store it in the Application and do it that way, if you REALLY had to, but I'm sure everyone and their mother will tell you that's a terrible idea.
Related
I need the calling activity to relaunch a child. that is, if the child activity already exist, close it then launch it again. and keeping the history/relationship between the parent and child. ie when I press back from the child I want it to go to the parent
CLEAR_TOP says that it will not relaunch an activity if it already exists
NEW_TASK sounds like it will make the child activity the root of the app which I don't want
According to my understanding you have 3 requirements:
1. There is only one instance of child activity:
I don't know the reason, but if you just want to ensure the singleton of child activity on current task top, then singleTop is fine; otherwise you need to use singleTask, but then child activity will be created at the root of another task.
2. If the child already exists, close it then launch it again:
For both singleTop and singleTask, if there is already an instance of child activity (for singleTop, already an instance on current task top), the intent will be delivered to that instance. You can reset your activity status in onNewInstance(). This should have the same effects of creating a new one.
3. When I press back from the child I want to go to the parent:
For singleTop, no need to make anymore effort.
For singleTask, you can try if this tricky works: If the child activity knows what is the next activity to show by pressing back, it can manually start the parent activity by calling startActivity() in onDestroy().
More information about Android lauchMode, read http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Use this, works for me
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
getApplicationContext().startActivity(intent);
If an activity calls it parent activity by startActivity(intent) // Here intent is parent activity,
Then, will a new instance of the parent activty will be opened or the same background activity will come in front?
I'm asking because my parent activity contains a ListView & my child activity is performing an operation that is updating the ListView in parent activity.
As soon as the user presses the Done button in child activity, the list needs to be updated. That's why I'm using startActivity.
Instead of using startActivity(intent) in your parent activity, Use startActivityForResult(intent).
Doing the way you intend to do will create lot of instances of parent+child activities in your activity stack and that is not good.
In the parent activity, you can just implement onActivityResult() method, and u can update your list.
And the answer to your question, Yes a new instance of the parent activity will be created if you do that.
I am writing an activity test for an activity we wrote with 3 buttons. 2 of these buttons start other activities.
I can write a test that simulates a button push and then checks if the desired activity is running, but I can't move back from that second activity. The second activity stays at the front and prevents the other tests, that assume the first activity is running, from working properly. They just kind of freeze.
I have a reference to the first activity, but it is the second activity I need to I guess call finish() on. Is there a way to do this?
EDIT: I added some actual source code illustrating my problem in this gist: https://gist.github.com/3076103
It is specifically about testing activities. In the production code everything is fine.
You should probably use http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html to get a reference of the second activity or you can block the second activity from being launched(Still you are guranteed that the call to start the second activity infact had reached till the framework).
You need a way for your activities to communicate with one another, so that one activity can tell the other to finish. There are several ways you can accomplish this. One method is to create a service within my application; my "second" activities would connect to this service to register a way to receive messages, and my primary activity would connect in order to provide them.
In Activity1 add the following to start Activity2
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
In Activity2 add the following to start Activity1 and finish Activity2
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
For more details: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
If you start the activity using startActivityForResult, then you can close the activity from parent using finishActivity(int requestCode).
To start the activity :
startActivityForResult(new Intent(...), 123123 /*requestCode*/);
And when you want to finish that activity (from caller), use :
finishActivity(123123 /*requestCode*/)
Also there is a way to find, whether child activity is finished or not. But you can track this only when child activity calls finish() for self. To receive the child finish request from the child, you need to override the finishFromChild() method in parent activity.
I have an activity which has a thread and a view in it...they're suspiciously similar to LunarLander. To show an in-game menu, i'm calling the startActivityForResult for a different activity which has a number of buttons on it...this is then returning the button type pressed to the parent activity. This is fine except when I carry on in the parent activity, the original thread I had is now TERMINATED. I guess this is happening because the parent activity has lost focus and so the thread is considered dead, but I want to carry on with the thread.
Is there any way I can keep the thread alive? I've tried setFlags on the intent for the new activity but i've not found anything that maintains it..
I gave up and decided to cheat. Instead of trying to return to the parent from the child activity, i'm recreating the parent activity again.
So my gameActivity creates the menuActivity and then is scrapped using the inGameMenuIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); flag. Then the menuActivity does its stuff and when a button is pressed it creates a new gameActivity and menuActivity is scrapped.
My android application has 1 main activity. And it launches some sub-activity (which I wrote) and that also launches some sub-activity (which I wrote). I do this:
Intent i = new Intent("my intent1");
startActivity(i);
My question is how can each of my sub-activity and sub-sub-activity get back to the Parent activity?
Thank you.
You should start your sub Activities via [startActivityForResult()][1], and when you're done in your sub-activity, call finish(). This will close the sub-activity and return to the Activity that called startActivityForResult.
[1]: http://developer.android.com/intl/de/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
I think there is a method getParent() which returns the parent Activity of a child activity.