1

I have MainActivty class with DrawerLayout, so I add Fragments dynamically.

public void selectDrawerItem(MenuItem menuItem) {
    Fragment fragment = null;
    Class fragmentClass;
    switch(menuItem.getItemId()) {
        case R.id.nav_websites:
            fragmentClass = ScreenOne.class;
            break;
        case R.id.nav_commands:
            fragmentClass = ScreenTwo.class;
            break;
        case R.id.nav_help:
            fragmentClass = ScreenThree.class;
            break;
        default:
            fragmentClass = ScreenOne.class;
    }
    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
    setTitle(menuItem.getTitle());
    mDrawerLayout.closeDrawers();
}

In first fragment

public class ScreenOne extends Fragment implements  Button.OnClickListener 

I have a function

public void loadText() {
    SharedPreferences myPrefs;
    myPrefs = getActivity().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);

    siteName1.setText(myPrefs.getString("siteName1_txt", "").toString());
    siteURL1.setText(myPrefs.getString("siteURL1_txt", "").toString());

}

In my mainActivity class, in onCreate method, I want to call loadText() method, I tried

ScreenOne fragment = new ScreenOne();
fragment.loadText();

But that didn't work. I can't find fragment by ID or Tag, because they don't havy any. Please, any help or advice woild by much appreciated.

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
fivievv
  • 35
  • 5

3 Answers3

1

There are several big problems with your code, these are the problems I see:

That being said, the easiest fix for this particular problem is to only call the loadText method when you are guaranteed that is the current screen (ie. do it when you change it OR keep a reference to the current item and use instanceOf)

public void selectDrawerItem(MenuItem menuItem) {
    ...
    try {
        fragment = (Fragment) fragmentClass.newInstance();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
        ...
        if (ScreenOne.class.equals(fragmentClass)) {
            fragment.loadText();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

However I want to make clear, this is not the cleanest solution and I recommend you stick with single responsibility and have the fragment be responsible for loading its own text, while the activity is responsible for passing in the value from shared preferences as an argument (or via an interface callback pattern). This avoids any change of loadText being called before the fragment is ready.

Community
  • 1
  • 1
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

You can access any public method of YourActivity by using like

((YourActivity) getActivity()).nameofmethod

only whom is attached to YourActivity

Rajesh
  • 2,618
  • 19
  • 25
  • As Darwin mentioned on the other answer - He is looking to call a function whiche lies in fragment from the activity and not vice versa – Nick Cardoso Mar 10 '17 at 13:15
-1

You can call your Activity method by using context of that activity such as:

((MainActivity)context).updateWishlistCount();

Hope this helps you!

Kunal Dudka
  • 467
  • 1
  • 6
  • 14
  • 1
    He is looking to call a function lies in fragment from the activity and not vice versa – darwin Mar 10 '17 at 13:06
  • Also in a fragment you would use getActivity. Also to decouple a fragment (it's entire point) you'd use an interface here. Also someone posted this identical answer a long time before you. – Nick Cardoso Mar 10 '17 at 13:31