Let say that I have two Activity to develop in Android. Upon the end user click a button in Activity A, the application supposed to pull data off a JSON API and present that information on Activity B.
So my question is, what's the best practice or pattern? i.e.
Activity A will call an AsyncTask and perform JSON call. Pull the data, push it into the Intent via putExtra, and call Activity B?
Activity A will call Activity B, Activity B onCreate will call an AsyncTask and perform the JSON call?
Other suggestions?
Which one is the prefer pattern?
And which gives better user experience? (e.g. imagine where the error dialog will be if connection fail to the server.)
I think normally something of your #2 approach is done. In most cases, though, you need to tell Activity B what type of information to request from the JSON API. So say in Activity A you are choosing an item from a list, and Activity B will get more information about that item. In this example Activity A will simply pass a reference to which item was selected, and then Activity B can use that reference to make a JSON request for more information about that item. Does that make sense?
I usually try to pass as little information as I can in Intent extras so I would steer clear of your first solution.
Related
I'm trying to pass the data from one activity to another activity without opening the activity. I tried it by not writing startActivity(intent) part but it fails.
How do I pass the data from one activity(1) to another activity(2) without opening that activity(2)?
Activities are UI elements so your question makes no sense. If you want perform some action that does not have UI create a service to perform the work and start or otherwise call that service from your first activity.
If you want to make data available to activity 2 when it does start, pick a persistent storage mechanism and write the data there, then read the data when activity 2 opens.
https://developer.android.com/guide/topics/data/data-storage
Alternatively, you can create a custom Application and store the data there and share it between activities.
Just a word of advice. You should describe what you are trying to accomplish not how you want to accomplish it. Activities are not the right tool for this task, but we can't suggest the right tool because we don't know what you want to build.
You can use events for passing around data. Either use the android native LocalBroadcastsSytem and put data as parcelable in the intent, or you can use any event library like EventBus
Pass the data from one activity to another activity without opening the activity try to do with the broadcast receiver or SharePhreferance or make that data public static You can access your data in anywhere of the application.
I have got 2 activities - "A" contains fragment with a list of conversations, "B" represents a conversation (shows it's messages), in B I can also send a new message.
I would like to be able to update A's list of conversations every time a message is sent in B, so when I click android's back button, A activity's view is updated.
The core of the problem for me, is that I'm not starting A through an Intent when I click the android's back button, so I don't know how to get this effect.
Thank you for your help!
When A is on the backstack, there's no guarantee that an instance of A even exists in memory. The answer to the question of how to manipulate A from B is don't.
Some correct ways of doing it:
If your model (the list of conversations) is Parcelable or Serializable, you can pass it between activities via Intent. You can pass it back from B to A if you start B for result and retrieve it from the Intent returned to A's onActivityResult.
Make the model persistent, like in a database or SharedPreferences file.
Put the model in a bound Service. This would be faster than having each activity load it from persistent storage, but you may still need to make it persistent so you don't lose it when the Service shuts down.
I don't know how to solve my problems. It goes like this:
In main activtiy I make my own objects and added them to list(arrayList) and after that when I go to another activity I would like to:
to send list of list og my own ojbects, I know you make it with: intent.putExtra but you don't have type list myObject
when I pass the list of lists of objects I think it makes new istance of these datas, but I would like to have one istance all the tame and I would like them to read and manipulate on first instance.
More explanation in my main activity I make objects and they are ready for all other activityes and all other activitys can read and write my list od lists. Only one activity is active in a time.
And I am also intetrested in when I manupilated the data in some activity and I would like to go to main menu and pick other activity how to send from that first activity to menu activity and pass them to next activity to process.
Would you help me, please. Best regards Robert
I suggest you implement an application object, and have the objects that you're referring to live in the application object rather than an activity object. There can be only application object associated with any particular app, and it gets created before any Activity objects, and is independent of whatever activity objects are created/destroyed during the lifetime of your app. Hence you don't need to worry about sharing your objects between activities, because they're all available globally in the application object.
Lets say I have 2 activities:
A: A ListView displaying articles titles. Data is fetched from a web server and converted from XML to a list of ArticleSummary. Only user titles and id are returned by the server. Click on a title starts activity B.
B: A form to edit an article. Article is fetched from server. When the user hits OK, modifications are sent to the server and activity closed.
When the user go back to activity A, I would like to update the article title without any additional web request.
I was thinking about the following solution:
When article is modified, send a broadcast event with article id and new attributes values.
Listen for this event on activity A
Update the ArticleSummary object
notify data changed on ListView
Is there a better approach ?
If you want to have a shared data model between different Activities, you can place it in an extension of the Application class. Or, you can use a singleton. Just reload the data from the shared location when the ListView activity is restarted.
As Fredley alluded to, if you have are communicating with a server you should be sure to do so in a separate background thread.
you can also use startActivityForResult() to launch activity and managed returned data.
Check the "Returning a Result from a Screen" section in the part below.
http://developer.android.com/guide/appendix/faq/commontasks.html
I have an Activity which is an OpenGL view. I also have an xml layout to use for preferences. Until now, to show the preference menu, I just brought it to front by setContentView(). And the same to get back to the OpenGL view.
But is this a case where I should give the preference menu its own Activity?
I guess this would make a few things much easier. For example, the back button would just work, opposed to now where I have to code it or it will just exits the application.
And if this is a good idea, how do I pass data both ways? I have a class that store all preferences. Can I send it to the Activity and back again? Or is the best way to store the preferences in a sqlite database and then use it for passing data?
I find it easier to segregate menus and such into separate activities (unless you are using dialogs etc..) As far as storing data you can do it a number of ways:
Database
StoredPreferences
Intent extras with putExtra/Bundle
Creating an application subclass and storing preferences there
Each have their merit. 4 is pretty easy as you just have to state the application class name in your manifest then call: MyAppClass app = (MyAppClass)getApplicationContext(); and you can then use any variables in MyAppClass via app. 2 is also straightforward.
You already pointed out the main difference: history management.
You can pass data to Activity via Intents putExtra()/getExtra():
Create an Intend and add custom data via Intent.putExtra(..)
Start the new Activity: startActivityForResult(intent).
Inside new Activity you can get extra data with intent.getXyzExtra() (where xyz is type).
When new Activity is done just call setResult(int, resultIntent). Again you can add extra data as described in 1.
Call finish() to end the activity.
In original Activity method onActivityResult will be called. Again extract data from Intent as described in 3.