【问题标题】:Android: Get instance of Fragments Activity from another ActivityAndroid:从另一个 Activity 获取 Fragments Activity 的实例
【发布时间】:2016-04-18 13:44:53
【问题描述】:

我希望从另一个活动 (RemoveStatement) 访问活动方法 (Budgeting.readFromDb())。 RemoveStatement 通过来自 BudgetingFragment(Budgeting 中的占位符片段)的意图运行。

但是,当我尝试获取 BudgetingFragment 的实例(通过 getFragmentManager().findFragmentByTag("budgetingFragment"))调用 getActivity() 时,它返回一个空对象。我似乎无法弄清楚它为什么会这样做以及如何解决它。

我尝试了以下方法:

  1. 给 backStack 添加标签
  2. 替换budgetingFragment 中的片段后调用getSupportFragmentManager().executePendingTransactions()。

任何解决此问题的帮助将不胜感激。

预算中更改片段的方法

public void onClickFragment(int id, String arrowDirection){
    //Get current Fragment
    Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.budgetingFragment);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


    if (currentFragment instanceof BudgetingOverviewFragment && arrowDirection == "Left"){
        IncomeFragment incomeFragment = new IncomeFragment();
        transaction.replace(R.id.budgetingFragment, incomeFragment, "budgetingFragment");

    }
    else if (currentFragment instanceof BudgetingOverviewFragment && arrowDirection == "Right"){
        ExpenseFragment expenseFragment = new ExpenseFragment();
        transaction.replace(R.id.budgetingFragment, expenseFragment, "budgetingFragment");
    }
    else if (currentFragment instanceof IncomeFragment && arrowDirection == "Right"){
        BudgetingOverviewFragment overviewFragment = new BudgetingOverviewFragment();
        transaction.replace(R.id.budgetingFragment, overviewFragment, "budgetingFragment");
    }
    else{
        BudgetingOverviewFragment overviewFragment = new BudgetingOverviewFragment();
        transaction.replace(R.id.budgetingFragment, overviewFragment, "budgetingFragment");
    }

    transaction.addToBackStack("budgetingFragment");
    transaction.commit();
    getSupportFragmentManager().executePendingTransactions();

}

RemoveStatements onCreate() 方法中的代码:

  FragmentManager fm = getFragmentManager();

  //This Fragment is a null pointer
  Fragment callingFragment = fm.findFragmentByTag("budgetingFragment");

  //This crashes the application as getActivity() is called on a null reference
  Budgeting budgeting = (Budgeting) callingFragment.getActivity();

【问题讨论】:

    标签: android android-fragments android-intent android-activity


    【解决方案1】:

    每个活动都有自己的 FragmentManager。所以你的第二个活动找不到任何"budgetingFragment"

    【讨论】:

    • 那么获取片段实例甚至预算活动实例的最佳方法是什么?是否建议将预算活动片段管理器的实例传递给 RemoveStatement
    • 片段不应该相互感知(松散耦合)。在宿主活动中为片段设置参数并使用callbacks
    【解决方案2】:

    据我所知,您的意图是将选择的 activity1 片段检索到 activity2 中,对吗?

    好吧,您不会通过查看 activity2 的片段管理器来获得它,因为它位于 activity1 的不同上下文中。

    推荐的方法是在 Intent 的定义(link1link2)期间聚集所选片段所需的所有数据并将其传递给 activity2

    activity2创建时,可以获取之前序列化的数据,在activity2上使用。此外,您可以使用 activity1 (link) 中的替换事务来扩充 activity2 上的新片段。请注意,为此,您需要在 activity2 的布局中提供 R.id.budgetingFragment FrameLayout id。

    【讨论】:

    • 我的总体意图是获取对 Budgeting 类对象的引用,以便我可以使用它的方法 readFromDb。我最初认为最简单的方法是通过在预算内的片段上调用 ​​getActivity(),因为该片段调用 RemoveStatement 类。我实际上对片段存储的数据并不感兴趣,而是对它的 getActivity() 方法感兴趣。我现在相信我可能更容易通过您建议的意图将此引用传递给预算类。但是,是否可以将 BudgetingFragment.getActivity() 作为 Intent 的参数传递?
    • 如果您创建一个 activityDB,由 DB 实例化和方法组成,将用于扩展 activity1activity2我>?有了这个,您可以访问所需的方法 - readFromDb() 在活动 1 和 2 上。
    • 我完全忽略了这个解决方案。非常感谢。我认为数据库没有在活动之间共享,但我设法使用 SQLite 开放助手单例共享。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多