1

我已经Bundle在几个 Android 回调方法中看到了 s 的恢复,但在许多情况下,在开发者Bundle网站上手动创建和设置s ,在这种情况下来自创建时的外部消息:Fragment

public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);

    return f;
}

例如,在 this other question中,在方法中恢复了捆绑数据onCreateView()

public class Frag2 extends Fragment {

    public View onCreateView(LayoutInflater inflater,
        ViewGroup containerObject,
        Bundle savedInstanceState){
        //here is your arguments
        Bundle bundle=getArguments(); 

        //here is your list array 
        String[] myStrings=bundle.getStringArray("elist");   
    }
}

我对Bundle每个回调方法与“其他包”提供的数据有点困惑:

Bundle bundle=getArguments(); 

以及检索这些不同类型的捆绑数据的正确方法和位置

提前致谢!

4

1 回答 1

1

上面描述的两种方法正是正确的方法

  • 初始化 的新实例Fragment并传递初始参数。
  • 检索Fragment.

换句话说,你在正确的轨道上!它应该工作,你应该对自己感到满意:)

编辑:

  • Bundle可以在或onCreateView()中 检索onCreate()。我更喜欢onCreate(),因为它代表Fragment实例的创建并且是初始化的正确位置。
  • 调用总是只有一个Bundle实例getArguments(),并且这个Bundle实例包含你所有的ints,Strings,等等。
于 2015-05-26T13:16:07.587 回答