将 Context 用于没有 MVC 的简单应用程序的最佳实践

Best Practice for using Context for a simple App without MVC

我是编程新手。我有一个只有很少活动的简单应用程序,我需要在这些活动中使用 Context。 link: https://openclassrooms.com/en/courses/4661936-develop-your-first-android-application/4679186-learn-the-model-view-controller-pattern and the answer in 说我不需要 MVC 来实现一个简单的应用程序,我想避免使用它。在我的案例中获取上下文的最佳做法是什么?我认为 static Context 会导致内存泄漏。我应该在每次需要上下文时只调用 getContext() 吗? (我测试过,它有效)。它不适用于 this,仅适用于 getContext()。我认为这是因为它在片段内部。谢谢

为了更好地理解:这是我所拥有的一部分:

public class MainApplication extends Application 
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        FirstManager.createInstance(getApplicationContext());
    }
}

我在构造函数的帮助下将此上下文传递给 FirstManager。如果我有比 FirstManager 多的 activities/classes,再写 getApplicationContext() 还是在 class 范围内写是更好的做法: Context context;onCreate 之后: getContext() 并将其保存到 context?

更新:这是片段(其他片段类似,没什么特别的):

public class List extends Fragment {
...
private FloatingActionButton fab;
    private FloatingActionButton fdb;
    private RecyclerView recyclerView;
...

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        fab = ( FloatingActionButton ) view.findViewById(R.id.floatingActionButton);
        recyclerView = (RecyclerView) view.findViewById(R.id.RView);
        fdb = ( FloatingActionButton ) view.findViewById(R.id.floatingDeleteButton);


            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(getContext(), FloatingButtonActivity.class));
                }
            });
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),1);
            recyclerView.addItemDecoration(dividerItemDecoration);
        }
 @Override
        public void onResume() {
            super.onResume();
            final RAdapter radapter = new RAdapter(getContext(),getActivity());
            recyclerView.setAdapter(radapter);

            fdb.hide();
            fdb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    radapter.deleteSelection();
                }
            });
        }
}

在每个 Fragment 中,您都可以使用 getContextgetActivity 并根据需要使用它们。请记住,两者都是 Nullable,如果尚未创建片段的根视图,则将是 null。一些示例代码:

@Override
public void onViewCreated(View view) {
    ...

    Context context = getContext();
    if (context != null) {
        startActivity(new Intent(context, FloatingButtonActivity.class));
        ...
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        ...
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context);
    }
}

每次使用这个局部变量和 getContext 之间没有性能差异,你只是摆脱了关于上下文为 null 的警告。

并且由于上下文不会暴露在生命周期超过片段或 Activity 的实体之外(这意味着您不会将上下文提供给任何可能存在的 class 实例在 Fragment 或 Activity 被杀死后),从这段代码中你不会有泄漏。