0

I am new for android development and I want to make simple application. I have an activity which is MainActivity and I have a fragment MainFragment inside of that activity. I want to change TextView of this fragment from Activity onCreate and/or onResume. However, I could not handle that. My onCreate method of activity is:

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment()).commit();
    }

    TextView tv = (TextView) getSupportFragmentManager().findFragmentById(R.layout.fragment_main).getView().findViewById(R.id.textView1);

}

I have NullPointerException while getting TextView in here. I assume that it cant find the fragment by findFragmentById(R.layout.fragment_main). Because I have fragment_main.xml which is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.deneme.MainFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
</RelativeLayout>

My MainFragment Class' onCreateView method is:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_main, container, false);

    return rootView;
}

I do not want to change text of textview of fragment in the onCreate method. I want to change from activity. What is my problem?

Thanks

Sami
  • 490
  • 6
  • 29
  • I think you could have a solution from it:http://stackoverflow.com/questions/16295101/how-to-change-fragments-textviews-text-from-activity – ridoy May 07 '14 at 19:59
  • When I do that, I get "_Cannot cast from Fragment to MainFragment_" error. and also I do not have any fragment tag in xml file so i dont have fragment id or something. This findFragmentById may not work because of that. What do you think? – Sami May 07 '14 at 20:11

2 Answers2

1

There are a couple of things you need to fix here. As veteran android folks will tell you, changing views of fragment from activity is not a good thing to do. Think of activity as the 'mother' of fragments it creates. If the mother starts doing the homework of its children then what's the children gonna do then? Fragment's views should be handled from inside the fragment only not outside. Further more since the fragment may not be visible in screen so the android kernel would decide to destroy it and GC it. If you keep on holding references to its view then its gonna be a waste of memory and resources.

But if you still wanna go that way then:

  • Make sure your fragment is created and bound to the parent using a fragment transaction from a fragment manger. Save a reference to the fragment object in your activity object. make sure that the transaction is committed. This action will make android call the oCreateView of fragment and there you can inflate the fragment's main view. Immediately save the reference to the main fragment view in a class property variable.
  • Subsequently later if you want to access the view of the fragment in your activity, call the getter for your view on the fragment's object in the activity and you are done.

But i still urge you to let fragments do their own view handling and not to mix view handling between activity and fragments and also not in between two of more fragments. that's a big design no no.

Nazgul
  • 1,892
  • 1
  • 11
  • 15
  • The reason I wanted to change in Activity is that I have another Activity which I make changes on PreferencesFragment and when I click back button I want to see those changes on my MainActivity. I know that when I click back button, onResume is called. So I wanted to change something in onResume method of MainActivity. How can I do that? – Sami May 07 '14 at 20:09
  • 1
    well if that is the case, and assuming that your MainFragment is the fragment visible int he activity, you can do that code in your onResume of fragment also. After Activity's onresume, fragment's one is also called. – Nazgul May 07 '14 at 20:14
  • Thanks!! Since I am not familiar with Fragment, I realised i have done something stupid :) this is the way how it should be done. – Sami May 07 '14 at 20:42
1

Well,

this is not what you want.

You should implement the update of the UI in the fragment as a public method, and call it from the Activity when you need it.

That is more elegant, and you should be able to achive anything with this pattern. If not, then that is the sign of the bad design of your components, and you should consider refactoring.

Update:

Its easy:)

YourFragment frag = (YourFragment)getSupportFragmentManager().findFragmentById(R.layout.fragment_main);

if(frag != null){
    frag.updateTextView();
}
kupsef
  • 3,357
  • 1
  • 21
  • 31
  • Actually my main concern is how to call it from Activity. I can write set methods of my textview but how am i gonna call it on activity? – Sami May 07 '14 at 20:13
  • You can access the Fragment with findFragmentById() as you already did in your posted code. All you have to do is to cast the found fragment to your concrete one, and call its public method. – kupsef May 07 '14 at 20:16