Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top