3

I am using a TextWatcher in my Activity at the onPostCreate but now I have turned it into a Fragment.

What's the equivalent of this in a Fragment?

  @Override
protected void onPostCreate(Bundle savedInstanceState) {
    mSearchView.addTextChangedListener(filterTextWatcher);
    super.onPostCreate(savedInstanceState);
}
Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
user3694470
  • 143
  • 1
  • 12
  • What do you intend to do? – Raghunandan Jun 14 '14 at 15:54
  • well if i want to explain, when for example flipping the phone, Android recreates the activity, and the automatic restoration of the state of the input fields, is happening after onCreate had finished, where the TextWatcher was added as a TextChangedListener.The solution to the problem consisted in adding the TextWatcher in onPostCreate, which is called after restoration has taken place. so i need to add some thing like OnPostCreate in Activity to use in fragment – user3694470 Jun 14 '14 at 15:59
  • look at the fragment lifecycle and you will know your self. http://developer.android.com/guide/components/fragments.html. `onActivityCreated` – Raghunandan Jun 14 '14 at 16:00
  • dude, can you also help me what to replace return ((ListViewActivity) mContext).new ListFilter(); with, considering ListViewActivity is a fragment – user3694470 Jun 14 '14 at 16:17
  • Use `ListFragment` if you want a list in fragment. Fragment is attached to the Activity. sure i will post it as an answer – Raghunandan Jun 14 '14 at 16:51
  • also http://developer.android.com/reference/android/app/Activity.html#onPostCreate%28android.os.Bundle%29 – Raghunandan Jun 14 '14 at 17:02
  • well its not only a list, so i doubt that i can – user3694470 Jun 14 '14 at 17:17
  • i don't get it. What has that gotta do with list? – Raghunandan Jun 15 '14 at 01:59
  • its for listviewfilter on textwatcher – user3694470 Jun 15 '14 at 08:55

1 Answers1

8

Look at the Fragment Lifecycle. Wait until the Fragment is attached to the Activity

http://developer.android.com/guide/components/fragments.html

You can do your work in onActivityCreated.

To get Context use getActivity()

protected void onPostCreate (Bundle savedInstanceState)

Added in API level 1

Called when Activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

Parameters savedInstanceState If the Activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
Raghunandan
  • 132,755
  • 26
  • 225
  • 256