0

I wish to access an activities method (Budgeting.readFromDb()) from another activity (RemoveStatement). RemoveStatement is ran through an intent from BudgetingFragment (a placeholder fragment within Budgeting).

However, when I try to get the an instance of the BudgetingFragment (through getFragmentManager().findFragmentByTag("budgetingFragment")) to call getActivity(), it returns a null object. I can't seem to figure out why it is doing this and how to fix it.

I have tried the following things:

  1. Add tag to backStack
  2. Call getSupportFragmentManager().executePendingTransactions() after replacing the fragment within budgetingFragment.

Any help to fix this would be appreciated.

Method within Budgeting to change fragment

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();

}

Code Within RemoveStatements onCreate() method:

  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();

2 Answers2

0

Every activity has it own FragmentManager. So your second activity cannot find any "budgetingFragment".

Maxim G
  • 1,489
  • 1
  • 13
  • 23
  • So what would be the best approach to get the instance of the fragment or even the instance of the Budgeting Activity? Would it be advisable to pass the instance of the Budgeting Activities fragment manager to RemoveStatement – JoeScott1232 Apr 18 '16 at 14:22
  • Fragments should not be aware each other (loose coupling). Set arguments to fragments in the host Activity and use [callbacks](http://developer.android.com/training/basics/fragments/communicating.html) – Maxim G Apr 18 '16 at 15:03
0

As far as I can see, your intention is to retrieve the chosen fragment of activity1 into the activity2, correct?

Well, you won't get it by looking at activity2's fragment manager because it's on a different context of activity1.

The recommended way is to agglomerate all data needed from the chosen fragment and pass it to activity2 during Intent's definition (link1 or link2).

When activity2 is created, you can get the data, previously serialized, thus use it on activity2. Also, you can inflate a new fragment on activity2 with a replace transaction as in activity1 (link). Note that, to do so, you'll need the R.id.budgetingFragment FrameLayout id to be available on activity2's layout.

Community
  • 1
  • 1
ReVs
  • 47
  • 5
  • My intention overall is to get a reference to the Budgeting class object so i can use its method readFromDb. I originally thought the easiest way of doing this was through calling getActivity() on the fragment within Budgeting as that fragment calls the RemoveStatement class. Im not actually interested in the data the fragment stores but its getActivity() method. I now believe i may be easier to pass this reference to the Budgeting class through the intent as you suggested. However, is it possible to pass BudgetingFragment.getActivity() as a parameter of the intent? – JoeScott1232 Apr 18 '16 at 16:57
  • What if you create an *activityDB*, composed with DB instantiation and methods, which will be used to extend *activity1* and *activity2*? With this you can access the desired method - readFromDb() on both activities 1 and 2. – ReVs Apr 18 '16 at 17:07
  • I completely overlooked that solution. Thank you very much. I thought that databases weren't shared between activities but i managed to share using a SQLite open helper singleton. – JoeScott1232 Apr 18 '16 at 17:43