I have recently started working with android. So I am in the initial stages of learning.
The question is: when we call startActivty(Intent) in the middle of execution of another activity.I actually thought that startActivity() will simply jump into the activity called. Isn't it the case? does it simply stacks the call to the activity?
I am getting this doubt bcoz..I actually have a program in which Activity A has a loop. Some where in the middle of loop Activity B is called. I want activity A to resume with the loop only when activity B finishes. I have made a call in A's loop like this:
in = new Intent(this,MyChoiceActivity.class);
in.putExtra("McObj", mc);
startActivity(in);
finish();
So What happens is Activity A calls B, but B is not entered, A simply resumes the loop and again calls B, simply the calls to B are stacked and once the loop in A completes, one by one calls to B in stack are executed so finally the first call to B is executed last(last in first out)...But I dont want the order to change...If A calls B, it should simply go and execute B and only then come to the loop..what should I do to accomplish this?
Your activity can be busy and just 'be there'. If you start a new activity while your activity is really doing something, it will not 'wait' until the other activity is finished: both can be around at the same time.
If it would do this, it would have you end up with a lot of busy-waiting processes, so luckily this is not what happens.
See the activity lifecycle to find out more about what an activity is (it is not just another class as you are using it now, it starts a separate thing that hangs around next to (not instead of / on top of) your current activity).
Instead, beacuse these things go rather asynchronous, you might want to use a different approach: use an ActivityForResult, and start a new one when you get that result.
Related
In my android app, there are two Activities A and B and there is a Button in A to launch B:
A -> B
I would like to launch an AsyncTask (which is an inner class of A) in the onCreate() method of A which will load some data. However, I don't want the user to be aware of that loading, that's why I launch the AsyncTask that way.
However, if A starts and the user launches B while the AsyncTask is executing, I was wondering if that would cause some Exceptions, bugs or something not right.
Is it possible to do that?
An AsyncTask always keeps a reference to the Activity, so you need to cancel the AT while destroying the Activity, ie onDestroy().
Here it's pretty well explained.
In an activity (say A) I have to perform a certain task depending on the result of another activity (B). I start activity B using startActivityForResult(). The pseudo code is something like this:
(in activity A)
//Statements
//startActivityForResult(activityB)
//get the result in some local variable for activity A. result is a boolean
//if(result==true) do something
//else do something else
Now the problem I'm facing is that after starting the activity B, it doesn't wait for the result to arrive from B. Instead, it proceeds and uses the default value of the boolean result.
Any solution?
If this particular block was in another thread, i could write a synchronized block and
issue a wait() after starting the activity B, and then notify() in the onActivityResult().
But since there's just a single thread, that's not an option right?
Should mention that activityB takes a user input of Yes/No and returns that. So on starting it, the result is not immediately available
Any solution?
Put your last two lines from your code listing in onActivityResult(), as is described in the Android documentation.
More importantly, you also need to rewrite activityB to actually follow the instructions for using setResult() to pass results back to activityA.
I'm trying to start an activity (Act2) from another activity(Act1), and it doesn't happen like it should.
In the debugger, I can see that as a result of calling startActivity() the method onCreate() of act2 is called 3 times (!!...), no error shows, or exception caught, and unfortunatly the desired UI doesn't show.
for trying to understand better where is the problem, I run the next 2 tests that showed expected normal behavior:
when I modify Act1 to start Act3 (instead of Act2, just for
understanding if its a problemof Act1), then act3 shows its UI as expected.
when Act4 start Act2, it runs as expected, and show the UI on the screen.
As to the code, I start the activity in the common way of:
Intent intent = new Intent(Act1.this, Act2.class);
startActivity(intent);
Anyone faced such thing?
Thanks.
You might want to read through the documentation on the Activity lifecycle.
OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.
To support this properly, you can save state information in onSaveInstanceState and restore it fron the state bundle you get in on create.
1. theNewIntent = new Intent(parentActivity,NewScreen.class);
2. parentActivity.startActivity(theNewIntent);
3. Log.d(TAG,"RETURNED BACK TO HOME VIEW");
Here i creates a new intent and starts a new activity. I want my current activity to be on hold until i finish the newly created activity. What i want is not to execute line 3 until "NewScreen" activity is finished.
Can anyone suggest me to do this.
You cant really achieve this without some kind of synchronization. The easiest thing to do would be to call startActivityForResult() and put your log message in onActivityResult(...). This would give you the synchronicity but would still kind of break your flow over 2 methods. The issue is that startActivity() is a non blocking call.
Your current activity will be on hold in the sense that it wont get any user feedback until the activity on top of it is cancelled.
I'm writing a simple Android app, and I'd like better control over the navigation/relationship between the activities. I don't want my activities to act like android activities...I don't want them to stack up within the Task. I want one Activity (let's call it MainActivity) to be the landing point and always be at the bottom of the stack, and I want only one instance of my second activity (call it SecondActivity) to be above it in the stack...would be nice to reuse it as well. I thought I could get this behavior by making MainActivity be the "main" Activity, and declare them both as launchMode=singleTop. This isn't working at all. I provide navigation between them using menus, so when I go back and forth a bunch of times and back out of the app, I go through the whole stack.
How's the best way to have fine control over the Task's Activity stack? I want MainActivity to always back out of the app, and SecondActivity to always back into a single instance of MainActivity. As well, I'd love to get singleTop working so I would use onNewIntent instead of creating and destroying every time. Using the manifest as well as the intent flag is just not working. Any ideas?
Well, you could always just call "finish()" within whatever Activity is calling another activity after the "startActivity()" call. I would definitely advise against trying to stuff an entire app into two activity classes and try to swap views based on what they're doing. If it's that important to you, just close your activities as you launch new ones (obviously not the MainActivity, though).