Android Firebase代码组织(概念)

Android Firebase代码组织(概念),android,firebase,firebase-realtime-database,firebase-authentication,code-organization,Android,Firebase,Firebase Realtime Database,Firebase Authentication,Code Organization,我正在使用Firebase开发我的第一个android应用程序。这也是我的第一个android应用程序,所以我没有过期,但已经有了一些编程语言,比如Java或PHP。我正在寻找专业建议,因为我的代码组织遇到了一些问题。我已经做了一些研究,但没有找到一个很好的解决办法 我的应用程序使用Firebase身份验证和Firebase实时数据库 我将解释我的问题的所有相关部分 主要活动… 检查用户是否已登录,如果未登录,则启动身份验证活动 包含一个负责数据库访问的对象DatabaseHandler,该

我正在使用Firebase开发我的第一个android应用程序。这也是我的第一个android应用程序,所以我没有过期,但已经有了一些编程语言,比如Java或PHP。我正在寻找专业建议,因为我的代码组织遇到了一些问题。我已经做了一些研究,但没有找到一个很好的解决办法

我的应用程序使用Firebase身份验证和Firebase实时数据库

我将解释我的问题的所有相关部分


主要活动…

  • 检查用户是否已登录,如果未登录,则启动身份验证活动
  • 包含一个负责数据库访问的对象
    DatabaseHandler
    ,该对象需要实例化一个对象
  • 包含一个
    FirebaseAuth
    对象,该对象附加了一个
    AuthStateListener
  • AuthStateListener
    检查用户是否已登录,如果已登录,则将使用当前用户重新实例化
    DatabaseHandler
    对象
  • 应该能够显示从
    数据库处理程序中检索到的一些数据

我的问题

我已经需要访问
MainActivity
onCreate()
方法中的
DatabaseHandler
对象来显示一些数据,但是由于
DatabaseHandler
在方法
onAuthStateChanged()
中实例化,为了确保已经有用户登录,这不起作用,因为在调用onAuthStateChanged()之前调用了
onCreate()


我的想法

我不完全确定如何解决这个问题,但我的第一个想法是重新构造我的项目,使我的主要活动只检查要调用哪些活动,而不是自己显示数据。我还在项目的早期阶段,所以这应该不是什么大问题。 我只是想知道这是否可行,或者是否有更好的解决方案


让我知道您的想法

这里是一个活动示例,其中的代码假定您要显示书籍列表:

public class MainActivity extends AppCompatActivity {
    private List<Book> bookList = new ArrayList<>();
    private RecyclerView recyclerView;
    private BookAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        adapter = new BookAdapter(bookList);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();
        if (user != null) {
            // User is still logged in
            // Get UserInfo and instantiate DatabaseHandler
            populateList();
        } else {
            // No user is logged in, go to auth activity
        }
    }

    private void populateList() {
        // Get Books from Firebase and add them to the adapter
        Book book = new Book();
        bookList.add(book);

        // Notify the adapter, so that it updates the UI
        adapter.notifyDataSetChanged();
    }
}
public类MainActivity扩展了AppCompatActivity{
private List bookList=new ArrayList();
私人回收站;
私人图书适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(recyclerView)findViewById(R.id.recycler\u视图);
适配器=新的BookAdapter(bookList);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
FirebaseAuth auth=FirebaseAuth.getInstance();
FirebaseUser=auth.getCurrentUser();
如果(用户!=null){
//用户仍在登录
//获取用户信息并实例化DatabaseHandler
大众主义者();
}否则{
//没有用户登录,请转到验证活动
}
}
私有void populateList(){
//从Firebase获取书籍并将其添加到适配器
书=新书();
图书目录。添加(图书);
//通知适配器,以便它更新UI
adapter.notifyDataSetChanged();
}
}
通过此操作,您可以执行以下操作:

  • onCreate
  • onCreate
    中检查用户,并采取相应的行动
  • populateList
    获取数据并显示它,这样就可以重新填充列表,而无需执行所有的
    onCreate
    操作(记住
    清除列表/适配器

下面是一个活动示例,其中的代码假定您要显示书籍列表:

public class MainActivity extends AppCompatActivity {
    private List<Book> bookList = new ArrayList<>();
    private RecyclerView recyclerView;
    private BookAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        adapter = new BookAdapter(bookList);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();
        if (user != null) {
            // User is still logged in
            // Get UserInfo and instantiate DatabaseHandler
            populateList();
        } else {
            // No user is logged in, go to auth activity
        }
    }

    private void populateList() {
        // Get Books from Firebase and add them to the adapter
        Book book = new Book();
        bookList.add(book);

        // Notify the adapter, so that it updates the UI
        adapter.notifyDataSetChanged();
    }
}
public类MainActivity扩展了AppCompatActivity{
private List bookList=new ArrayList();
私人回收站;
私人图书适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(recyclerView)findViewById(R.id.recycler\u视图);
适配器=新的BookAdapter(bookList);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
FirebaseAuth auth=FirebaseAuth.getInstance();
FirebaseUser=auth.getCurrentUser();
如果(用户!=null){
//用户仍在登录
//获取用户信息并实例化DatabaseHandler
大众主义者();
}否则{
//没有用户登录,请转到验证活动
}
}
私有void populateList(){
//从Firebase获取书籍并将其添加到适配器
书=新书();
图书目录。添加(图书);
//通知适配器,以便它更新UI
adapter.notifyDataSetChanged();
}
}
通过此操作,您可以执行以下操作:

  • onCreate
  • onCreate
    中检查用户,并采取相应的行动
  • populateList
    获取数据并显示它,这样就可以重新填充列表,而无需执行所有的
    onCreate
    操作(记住
    清除列表/适配器

据我所知,您不必在
onCreate
中获取和显示数据。您可以从活动中的另一个自定义方法更新UI。您可以