4

I have two activities, Activity A and Activity B. Activity B is creating a database, inserting data to it, editing data and then closing the database. Activity A is an empty screen and it calls activity B. Activity B finishes its database work and goes back to activity A. Now I want my activity A to access the database created and modified by Activity B. It might be a very simple solution, please excuse me if it is a silly question. Any clue will is highly appreciated.

Thanks, Shaista

4 Answers 4

2

To access the data in Activity A, use the onActivityResult() in Activity A in the following way:

@Override protected void onActivityResult(int requestCode, int resultCode, ntent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == PICK_CONTACT_REQUEST && resultCode == 10) { pos = intent.getIntExtra("doc_id", 1); mDbHelper.open(); /* Write the database accessing code and whatever you want to do on returning back from Activity B here. */ mDbHelper.close(); } } 

And also remember that you can use this method with the intent method with calling startActivityForResult() with intent That's it, what you have to do

If there is any confusion then you can ask

EDIT: This code will help in fetching the data

public class FirstActivity extends Activity { AnyDBAdapter mDbHelper = new AnyDBAdapter(this); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mDbHelper.open(); Cursor foemiCursor = mDbHelper.fetchAll();/*fetchAll() is which you need to create in AnyDBAdapter class showing query for fetching the database */ startManagingCursor(foemiCursor); /*Now the query will get executed and you need to access just vales from it, here you write code for it*/ foemiCursor.close(); mDbHelper.close(); } 
Sign up to request clarification or add additional context in comments.

14 Comments

Okay, this looks interesting thanks. Let me try it and will get back to you.
@Shaista Naaz : I hope this would be helpful for u
yes sure will do that but I am still trying to solve it. thanks anyways.
Hi Nikki, I still feel that there may be more simpler way. Like the database is created and now I want to check if the database is present in application ( but not in the same activity ) then check the database with the name that is the name of databse and if it exists then open it. I do not know if it possible but if it is then that would be the simplest approach I believe. I might be wrong also. not sure...
@Shaista Naaz : Do you have used different class for creating database
|
1

You should make a DBAdapter class to handle all of your database interaction. The database is SQL Lite and is for the whole application. Just access it in B as you did in A.

3 Comments

okay, I have created a DBAAdapter class and I am doing all the operation in activity B. In activity A I want to check if db does not exist then do not do anything and if it exists then access it . it is like basically just opening a file it is already there and else do not do anything.
I got the solution it was very simple as you had explained but apart from this I had to make my activity to be a single task. this was very important. 'DBAapter db = new DBAapter(this);' 'db.open();' Then in the manifest I had to use 'android:launchMode="singleTask"' Bingo!!! the problem is solved.
Awesome. I should have posted code but did not have my files with me. The search though was probably a better learning experience though. Glad it helped.
1

use content provider...But directly your activity cannot interact with content provider activity interact with content Resolver and than resolver interact with content provider so now you can access data of other activity.

1 Comment

is it possible that I check if there is a database existing in the application specifically in other activity then just access it, by calling db.open() or something like that ?
1

You should check this link, http://kagii.squarespace.com/journal/2010/9/10/android-sqlite-locking.html. It contains some good information on how to use databases on android.

1 Comment

Here in the example the code has one activity and one helper class. but I have multiple activity and one helper class and I want to access same database from everywhere. I do not want to create a new one. Only open the database if already created in another activity and access it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.