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
Related
I have an application which stores data in SQLite database. Main activity is just a screen with a single button. When user clicks this buton the second activity opens. The second activity is a list (ListActivity) which should contain all the records from database. Here is the question: which approaches fits better: 1) when button is clicked, start AsyncTask, in doInBackground method pull data from database and send pulled data to the second activity as a parcelable array or 2) start the second activity, in onCreate method pull the data from database and display progress bar while selecting data. I believe it is not based on primary opinion and there should be valuable pros and cons. But I am new to Android and I'm struggling to identify better approach for this. Thanks for your attention.
First approach is not user friendly. Because when user press a button to open different activity(In terms of UI), then user except to show a transition, Not the stuck in first activity. Actually it depend on your application UX design. Also first approach has another problem. If you load data in first activity then you have to transfer many data when you call another activity.
Use the slightly modified second approach. First start the second activity, then call the AsyncTask(in onCreate method) to load the data.
Well, I think the best is starting AsyncTask on clicking button and process data in doInBackground as You said. In onCreate() of second acivity display progress bar, and in doInBackground() send data and display items. It seems to be the best from point of performance.
The problem is only when my activity already shown it begins updates and it look no good. How it works: in onStart of activity I send cmd to service to get update data, also I register brodcast listener there. I want to prepare received data from service to show before activity appears. How to do that? Thanks.
How it works now: when I back from another activity first I see old data and then it changes (very fast but you can see it) to new.
if you want to setup things before your activity is shown you need to do things in the onCreate method instead of onStart method.
further informations in android documentation
When you send the command to the service to update your data, you should change the views in your Activity to show loading indicators, or a blank view, or whatever you want the user to see until the Broadcast comes in that your new data is ready. Then you shouldn't need to worry about old data being visible.
Is there any way I can go from one activity to another and delay the display of the screen on the target activity?
What I want to be able to do is to allow the target activity to fetch its required data but not to display anything until does.
I want the screen of the source activity to still be visible until I am ready with the data in the second activity.
Specifically, I am using an AsyncTask to fetch the data.
I know I could fetch the data in the source activity and then send it on to the target activity but this is not viable in our case.
Edit: More Info:
The whole reason I want this is because I am trying to change the structure of certain parts of the current code.
At present the way it works is the the first activity gets the data and then sends it to the second activity as a bundle.
This created problems when the second activity could be invoked from multiple places. It resulted in loads of duplicate code.
So, I decided to move the fetching of the data into the target activity thus cutting out any need for repeating code.
it also makes more sense for the activity to fetch its own data rather than relying on something else sending it.
You should first make a service that runs your async task. Then, start the service from your first activity with startService(new Intent(this, UpdaterServiceManager.class));
When the task ends in the service, start the second activity.
Click here for an excellent service tutorial.
Try to use service for this purpose.
I have implemented a search-function in my sourcecode which will be started when the user clicks the search-button on his device or the one in my actionbar. If he clicks the one on my actionbar I simply call onSearchRequested. Now here is my problem:
When the Search is done I display the results in a ListView. If the user clicks on an Item I want do go back to my MainActivity (which will always exist when the app isn't finished) and modify some values there und call a method of Main.
I know that there is startActivityForResult but I don't know how I could implement that because if the User hits the search button of his device the app automatically calls onSearchRequested... So the only solution that comes to my mind would be to implement a static method in my Main-Activity which I can call from within the Activity displaying my search results. But that isn't such a nice solution (or?)!
To summarize my question: is there an elegant way to get back from my activity displaying my search results to my main-activity and transfer data without startActivityForResult or implementing a static method in Main?
Thank u very much!
Implementing startActivityForResult is pretty easy. This might help you.
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.