How to transfer an object to a new intention in Android

I am very grateful for any thoughts on the following problem: In Android, I have my MainActivity, which creates and sets up a database handler class. eg.

public class DbHandler extends SQLiteOpenHelper{ //do db handling } 

In addition, I created my OnClickListener , which creates an Intent , then startActivity the Intent.

My question / problem is how best to pass DBHandler to the new Activity . I was thinking about creating a global one - and about the risks of restarting a thread. I cannot completely decide how to send / serialize if I do not create a shell - but still the problem of transferring an object in a "send"

I want to understand how others decided it? Thank you very much.

+4
source share
1 answer

As I know, it’s good practice to use only one instance of SQLiteOpenHelper, so create it as singleton and make it available

 public final class DatabaseHelper extends SQLiteOpenHelper { /** * instance. */ private static DatabaseHelper instance; /** * @return instance. */ public static synchronized DatabaseHelper getInstance() { if (instance == null) { instance = new DatabaseHelper(); } return instance; } ... } 
+2
source

All Articles