I have a simple application consisting in a contacts database (using a SQLiteDatabase class).
In my main activity layout, there is a "View Contacts" button. When pressed, I create a new activity (let's call it "ContactsActivity") in order to show a LinearLayout with the contacts data.
My question is: how can I give ContactsActivity access to the database?
Do I pass some data from the main activity (which I think is not an option)?
Do I just kind of open the database from this activity?
Do I use the same method that I used for the main activity (I mean, using a SQLiteOpenHelper and so)?
I don't know if I am giving enough information; otherwise, just ask me and I will provide any necessary info.
Thank you.
Related
Hello Everyone,
I had do a search on activity retain state.So I am getting to options/solutions for that:-
Using savedInstanceState() and retainInstanceState() methods.
Using Parent Child hierarchy declare in manifest and its working.
(Example Url:-How can I return to a parent activity correctly? I had same problem mention in Note scetion of the answer.That case is match with my problem.
So,I want to retain the activity states just like Whats app(call/chats/contacts fragments).
But In My scenario,I am fetching the contacts from server.So how can I persist my data while switching between chat and my fragment activity?
So while timing to fetch new data of my contact list from server.I want to save ui of my listview/recyclerview with old data previously I had.
Also suggest which method is good from above two methods or need to implement in other way.
i currently following tutorial making notepad, this tutorial using 2 activity main activity and edit activity, edit activity is using to fill data for database , but the query is executed in main activity
so the data must sent back OnActivityResult from edit activity to main activity .
the main question is i want to know why we must pass data to main activity instead of execute insert or update query on edit Activity, is this the best way?,
Can anyone explain why?
Thanks
There you have the 2 activities, one (Main) is meant ust to display the notes, the other (Edit) will let the user to create the note. When the user confirms the creation of the new note in the EditActivity, it will finish and in the MainActivity's "onActivityResult" the notes will be loaded and displayed in the list. So if you want to separate the creation from the visualization of the (I highly suggest you to do this), you want this approach.
i have an application with many Actvities , i need to call each of those activities with paramaters to retrieve data from database or a file , but if i call an activity a second time i dont want that the activity retrieve data again cos it can be boring for users .
Example :
i have main activity with menu that can call 3 activities : A,B and C
each one of them need parameters to access database
in each activity i have a link to navigate between them , i need to
call back the activities then from stack so no need that they access
database again.
Any suggestions are greatly appreciated.
If you are not finishing your activities, data will persist. You dont have to do anything additional.
Otherwise you can extend Application class and save your data into some data structure there, which you can retrieve any time later.
I've a big questionaire to fill, so I've decide to split it in two activities/screens. My question is:
- Should I send the data from activity1 to activity2 (startActivityForResult) and insert all the data on sqlite with an insert on activity2?
- Should I make an insert on activity1 and an update on activity2 since I know the row _id?
The option 1 is better to ensure the user fills all the questionaire, right?
Have you already considered:
Using shared preferences in activity 1, read it from activity 2, then insert; or,
Using a static array or map, which both activities can access?
I don't know what your security needs are but either two options seem so much simpler, especially number 2.
First option is better, because with this you will never have incomplete information in database (what happens if user exits from app when he finishes with first screen?) and you only will make a unique call to insert data.
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.