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.
Related
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.
I have two Activities in my app, a "Main" and a "Prompt" Activity. The Prompt is called from a button on the ActionBar in the Main Activity, and the Prompt has several EditText objects and a Spinner. The Prompt also has a Button that, when pressed, should validate the values in these objects and then send them back to the Main Activity.
I know you can send this information as a Bundle by placing it in an Intent and calling StartActivity. However, if I understand the Activity lifecycle, doing a startActivity() call to start the Main Activity will keep the old version of Main on the backstack and take up unneeded resources, as well as make it possible for the user to "clear" back to Prompt and then to the old Main Activity as well.
I want both the old Main activity as well as the Prompt activity to be removed from the backstack while also sending the values from Prompt's fields to a new Main activity. Can anyone help me figure out the most ideal way to accomplish this?
Thank you!
The best way to do this would be to start Prompt with startActivityForResult() then return the values to onActivityResult()
Docs with example
If for some reason this won't work for you then you can probably find an Intent flag that will accomplish what you need depending on your situation. One that may work for you is FLAG_ACTIVITY_REORDER_TO_FRONT This will bring your MainActivity to the top of the stack
Also, as long as you call finish() on your PromptActivity after calling startActivity then it will be cleared from the stack
You should watch Google I/O Navigation. It explains navigation and how the stack works very well
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.
I suppose the title is a bit confusing but here is what I'm trying to do:
I have a class called ManageClass which lists the entries of a database. I have written another private class within ManageClass which extends AsyncTask so that I can display a progress dialog while I'm getting the data from the database. Now when I click on an item I create a new Intent which takes me to my ViewItem class. I've added a button there so that the user can delete that particular entry that he/she is looking at. All of the above work fine.
Now I want after deleting that entry to kill the activity and go back to the previous one (the one displaying the list) but I want to refresh the listings.
My problem is that I cant use onResume() cause it will also be called when the activity is resumed after the AsyncTask finishes.
Could anyone help me with that? I'm really stuck... all ideas are welcome!!!
If I understand your app workflow you should use startActivityForResult instead of launching a new Activity via intent.
Look at here fore some example
Basically you can launch a new Activity and wait for a result via callback on the "opener" activity. so you can avoid to put your logic into onResume method
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).