Passing database to another activity via intent
I think it is good practice to have your database connection as a separate static class and call functions from other classes inside an AsyncTask whenever needed. Call your query function in doInBackground() and start your activity from onPostExecute() if you need to.
Here is the example:
create.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // initialize your database here db = new SQLDatabase(Main.this) boolean isInserted; new AsyncTask(){ @Override protected void onPreExecute() { // do things you need to do before calling db function } @Override protected Object doInBackground(Object[] params) { //call db function here isInserted = db.insertDataS("S01", "Smith", "London" ); //return query resul return isInserted; } @Override protected void onPostExecute(Object o) { // you can get returned result here as an object and cast it isInserted = (boolean)o; // then start your activity here if you need to or make changes // to a view or show a toast Intent i = new Intent(Main.this, otherClass.class); i.putExtras("result",isInserted); startActivity(i); } }.execute();
I hope this helps you.
use thisActivity 1:
Intent i = new Intent(MainActivity.this,SecondActivity.class);i.putExtra("anyname",db.Display(2, 1));startActivity(i);
Activity 2:
Intent i = getIntent(); String fName = i.getStringExtra("fone"); tv1.setText(fName);