简体   繁体   English

Android中的活动/片段模式

[英]Pattern for Activity / Fragment in android

I've a activity which basically is : 我的活动基本上是:

public class FragmentContainer extends FragmentActivityBase implements IRefreshListener {
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if (getIntent().getExtras() == null
                    || getIntent().getExtras().get("type") == null) {

              showProductList();
            }
            else
            {
            if (getIntent().getExtras().get("type").equals("customer"))
                    showCustomerList();
    }

    @Override
    public void showProductList() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        // load the product list
        ProductList fragment = new ProductList();

        fragmentTransaction.replace(R.id.fragment_container, fragment)
                .addToBackStack(null);
        fragmentTransaction.commit();
    }

    .....
}

in the fragment, I use onCreateView to get intent and then I create my view. 在片段中,我使用onCreateView获取意图,然后创建视图。 If I need to change the fragment, I get the reference to the parent Activity (taken from onAttach) and I call method referenced by the IRefreshListener. 如果需要更改片段,可以获取对父Activity的引用(取自onAttach),并调用IRefreshListener引用的方法。

like : 喜欢 :

IRefreshListener mCallback;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception.
    try {
        mCallback = (IRefreshListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement IRefreshListener");
    }
}


public void callCustomer() {
  mCallback.showCustomerList();
}

It works but whne I change the orientation, even I use setRetainInstance(true) it will be reseted. 它可以工作,但是当我更改方向时,即使我使用setRetainInstance(true)也将被重置。

I have 2 questions : 我有两个问题:

  1. Do I use the good pattern to manage my application. 我是否使用好的模式来管理我的应用程序。 The big activity which contains one fragment become bigger with the time 随着时间的流逝,包含一个碎片的大活动变得越来越大
  2. How should I handle orientation change ? 我应该如何应对方向改变?

Regards 问候

I do not find this pattern is more perfect or best one, although it is or was a suggestion from Google. 尽管这是Google的建议,但我认为这种模式不是最完美或最好的模式。 Because it could be a worse coding style if fragment knows particular activity or listeners, you might write more and more code, when you wanna to let your fragment know more its "container" or "parents". 因为如果片段知道特定的活动或侦听器,这可能是较差的编码风格,所以当您想让您的片段更多地了解其“容器”或“父母”时,您可能会编写越来越多的代码。 Will the fragment later be used for other activity which has not been implemented with IRefreshListener etc, you will code much more. 稍后该片段将用于尚未由IRefreshListener等实现的其他活动,您将编写更多代码。

My introduce is using Otto-Bus or Event-Bus. 我的介绍是使用Otto-Bus或Event-Bus。 You can just send message from one to one. 您可以一对一发送消息。 Every one doesn't have to know each other. 每个人都不必彼此认识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM