Not sure how to do this:
Current activity is: A
I want to start activity: B
But I want that activity B's UI should load completely before Activity A is finished.
What I need:
Current activity is: A
Start new activity: B
Activity B loads completely
Activity A finishes
I need this because activity B's UI is translucent when it starts. After an animation, the activity's background becomes opaque. While this is happening, the homescreen is showing because activity A finishes quickly.
Thank you in advance.
Here is one not-so-straightforward approach at the top of my head. You can use something called LocalBroadcast Manager. It would be like a message from Activity B to Activity A saying that 'Hey, I've finished loading the animation. Now I don't need you!".
So, before starting a new activity your activity A can start listening for this local broadcast and register a receiver. Then when on your activity B the animation finishes, you can send a Local Broadcast message saying "I don't need you"(not literally). This will be received by receiver in Activity A, where you can finish it.
See how to use LocalBroadcastManager? for how to implement it easily. Hope it helps you.
Related
I'm new to Android. My problem is:
Activity A calls activity B in the middle of its execution
Activity B must start and complete its execution
Only then activity A must resume (not restart)
Activity A has a loop. Somewhere in the middle of loop, Activity B is called, and once B finishes, A should resume the loop from where it left off.
I tried to code this, but what happens right now is that Activity A calls B, but B is not entered, then A resumes the loop and again calls B.
Essentially, the calls to B are stacked and once the loop in A completes, one by one the calls to B in stack are executed, so finally the first call to B is executed last in a last-in first-out order. I just want to execute B once, at the appropriate time.
Can anyone help me?
You are using the wrong design paradigm for your app. You should not start another activity like that. Keep the code on separate modules by all means, but do not start another activity just to transfer control.
Your activity will resume, when calling the Acticity B, don't finish Activity A. If you need to write some code when it resumes. you can do that by overriding onResume method of Activity A.
Take a look at Starting Activity for result. You can start Activity B for some result, and when Activity B finishes, the onActivityResult method of Activity A will be called and you can set it to continue/start work there.
EDIT
Based on your updated question you can try:
Create a method in Activity A which starts a loop
When you decide you want to call Activity B
2.1 Exit the loop, and save to where the loop is
2.2 Start Activity B for result
When Activity B finishes and returns you some kind of result, you will restart the loop (form the point you saved in step 2.1) in Activity A
In my app I have one main activity and second activity. My second activity is transparent and the main activity is shown under.
My problem is that sometimes when the user gets back to the app strait to the second activity, the system has destroyed the main activity and now the main activity is not shown throw the transparent part.
I want to make sure that if My main activity is destroyed by the system, then I should know about it or the app should restart from the main activity.
Is it possible? How?
When your second activity goes into onPause, kill it with finish().
Then use your MainActivity as a 'controller' and when onResume is called , i.e. when a user returns to your app start an Intent for your SecondActivity , that way you can always guarantee they are both there
I have a first activity A where I start a new activity B with:
intent.setClass(A.this, B.class);
Now, after some event, I would like to finish B from A (A contains a running thread).
How could I reach this?
Thanks in advance.
you can send broadcast message from A to B
Do you need to use B as Activity instance in these terms? may be better choice is to create B as dialog?
You don't. The Activity will be finished by the OS when memory resources are low.
Also, clicking the back button calls finish() on the current activity and displays the top activity in the backstack, so presumably you will have already finished the child.
Can someone help me replicate the following scenario:
I'm having a activity A with some specific tasks from which the app goes to activity B. When the user goes to B and presses back the app should close (maybe meaning that activity A performed finish()). But in some cases the app (not on the user action) goes back to Activity A, with the state preserved to when it went to B. The thing is that the app can never know if it should preserve activity A to be re-displayed later or not.
Hope I made it clear. Many thanks
You can start the Activity B with startActivityForResult() and in function of the result, when we come back to the activity A, Activity A finish() himself or not.
You can populate your Activity A every time in its onResume(). For persistence you can use SharedPreferences.
You can setup a BroadcastReceiver in Activity A which will call finish() for Activity A upon receiving a message. In the Activity B just before you close you then send that message to Activity A's broadcast receiver.
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.