1

saveFragmentInstanceState()您能否建议我在按下后退按钮时如何实现或其他一些方法来检索片段实例。我使用自己的堆栈作为片段,即代码如下:

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

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

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

    }
}

接下来是ActivityInTab类的实现。我认为这个类必须实现保存和检索片段状态的方法,但我仍然找不到如何做到这一点的方法。

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();
        }
    }
}

最后,这是 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);
    }
}

我只能使用 保存方向更改的实例状态onSaveInstanceState(Bundle outState),但不能保存后退按钮。

我将非常感谢任何建议,谢谢。

4

1 回答 1

1

在 onBackPressed 方法中,您可以访问 FragmentManager,它可以使用 FragmentManager 方法“findFragmentById”或“findFragmentByTag”为您检索任何片段。

根据添加片段的方式,您可以使用这两种方法中的任何一种直接访问要保存状态的任何片段。

于 2012-12-16T20:00:07.883 回答