How to access a database created in one action from another type of activity?

I have two types of activity: Activity A and Activity B. Activity B creates a database by inserting data into it, editing data and closing the database. Activity A is a blank screen and it triggers activity B. Activity B terminates the database and returns to activity A. Now I want my activity A to have access to the database created and modified by Activity B. It can be very simple solution please excuse me if this is a stupid question. Any hints would be greatly appreciated.

Thanks, Shaist

+4
source share
4 answers

You must make a DBAdapter class to handle all database interactions. The database is SQL Lite and is designed for the entire application. Just enter it in B, as you did in A.

+1
source

To access data in Activity A, use onActivityResult () in step A as follows:

@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 intention method with a call to startActivityForResult() with the intention that this is what you should do

If there is any confusion, you may ask

EDIT: this code will help in getting 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(); } 
+2
source

use a content provider ... But directly, your activity cannot interact with the activity of the content provider, interacting with Resolver content, and not with the converter interacting with the content provider, so now you can access data from other activities.

+1
source

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 Android databases.

+1
source

Source: https://habr.com/ru/post/1342983/


All Articles