Question

Could you anyone advice me how to implement saveFragmentInstanceState() or some other methods to retrieving fragment instance when back button is pressed. I use own stack for fragment, viz code bellow:

public class stackA extends ActivityInTab {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("StackA", "onCreate");

        if(savedInstanceState == null){
            navigateTo(new fragmentA());
        }

    }
}

Next there is implementation of ActivityInTab class. I think for this class must be implemented methods for saving and retrieving fragment state but still I can't find the way how to do this.

abstract class ActivityInTab extends FragmentActivity { 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_tab);

        Log.i("ActivityInTab", "onCreate");
    }

    protected void navigateTo(Fragment newFragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.content, newFragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }
    @Override
    public void onBackPressed() {

        Log.i("ActivityInTab", "onBackPressed");

        FragmentManager manager = getSupportFragmentManager();
        if (manager.getBackStackEntryCount() > 1) {
            super.onBackPressed();
        } else {            
            // Otherwise, ask user if he wants to leave :)
            //showExitDialog();
            super.onBackPressed();
        }
    }
}

And finally, this is imlementation of Fragments:

public class fragmentA extends Fragment {

    private LinearLayout ll;
    private FragmentActivity fa;

    private String textViewText;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Log.i("Fragment A", "onCreateView");

        fa = super.getActivity();
        ll = (LinearLayout) inflater.inflate(R.layout.fragmenta, container,
                false);
        Button next = (Button) ll.findViewById(R.id.button1);
        Button randomBtn = (Button) ll.findViewById(R.id.random_button);
        final TextView randomText = (TextView) ll
                .findViewById(R.id.random_textview);
        next.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view) {

                            ((ActivityInTab) getActivity()).navigateTo(new                    
                            fragmentB());
            }
        });

        randomBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                randomText.setText(String.valueOf(Math.random()));
                textViewText = randomText.getText().toString();
            }
        });

        if (savedInstanceState != null) {
            randomText.setText(savedInstanceState.getString("TextView"));
        }

        return ll;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // setRetainInstance(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("TextView", textViewText);
    }
}

I'm able to save instance state only for orientation changes using onSaveInstanceState(Bundle outState), but no for back button.

I would be very grateful for any advice, thanks.

Était-ce utile?

La solution

In the onBackPressed Method you have access to the FragmentManager which can retrieve any fragment for you using the FragmentManager methods "findFragmentById" or "findFragmentByTag".

You can get direct access to any fragment whos state you want to save using either of those two methods depending on how you added the fragments.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top