سؤال

I have an Activity with a FrameLayout and need to show different fragments based on user input.

The code I use for showing a fragment is this:

private void showFragment(Fragment fragment, Bundle args, boolean addToBackStack) {

    if (args != null) {
        fragment.setArguments(args);
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.setCustomAnimations(R.anim.activity_open_translate, R.anim.activity_close_scale);

    fragmentTransaction.replace(R.id.main_frame, fragment);

    if (addToBackStack) {
        fragmentTransaction.addToBackStack(fragment.getClass().getName());
    }
    fragmentTransaction.commit();
}

This is called as :

if (contactPickFragment == null) {
    contactPickFragment = new ContactPickFragment();
}
showFragment(contactPickFragment, args, true);

All this works fine. Now if the user goes into one fragment presses back and returns back to the same fragment, all my views inside stay the same. For example, I have an EditText inside the fragment and the user edits something inside. If the user comes back to the fragment, the same text persists. I do not want this to happen. How do I reset everything in the view?

I have added code within the Fragment's onCreateView() to clear the text, and from debugging I see that this is being called, but the text never gets cleared. What am I doing wrong here?

هل كانت مفيدة؟

المحلول

If you don't want the data from the previous instance to appear, simply create a new instance of ContactPickFragment each time you show it.

Clearing data in onCreateView() has no effect because view state is restored AFTER onCreateView(). Your Fragment has no view before onCreateView() and so Android cannot possibly apply the previous state any earlier. Values set on the views during onCreateView() will be overwritten by their previous values.

نصائح أخرى

As a general answer, there is no way to "refresh" the view of a Fragment, other than replacing the fragment with another instance of itself (possibly initialized with the parameters that you want to refresh/update).

You can reuse your fragments and refresh the state of your views. You just can't do it from onCreateView as @antonyt correctly points out.

Instead, override onViewStateRestored and set up the state of your views the way you'd like from there.

Something like:

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    View view = getView();

    // Code to call view.findViewById to grab the views you want
    // and set them to a specific state goes here
}

There are advantages to reusing fragments. Not the least of which is that if you have a memory leak with your fragment (which is easier than you may think to accomplish,) you will exacerbate the problem by creating myriads of them.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top