onStop()在定向改变娱乐后神秘地调用

时间:2014-09-12 18:42:12

标签: android android-fragments android-service screen-orientation

我不知道为什么但是在重新创建活动和片段的方向更改后,系统会停止片段。我在TabListener的操作栏上使用了标签片段。

日志:

09-09 16:49:36.132: I/fraglifecycle(1432): ChatFragment.onCreateView();
09-09 16:49:36.284: I/fraglifecycle(1432): ChatFragment.onServiceConnected();
09-09 16:49:36.288: I/fraglifecycle(1432): recuperando posição da listview: 2
================================= ROTATION ==================================
09-09 16:49:56.928: I/fraglifecycle(1432): onSaveInstanceState()
09-09 16:49:56.932: I/fraglifecycle(1432): ChatFragment.onStop();
09-09 16:49:57.028: I/fraglifecycle(1432): ChatFragment.onCreateView();
09-09 16:49:57.280: I/fraglifecycle(1432): tab selected; mFragment != null, attaching
09-09 16:49:57.284: I/fraglifecycle(1432): ChatFragment.onServiceConnected();
09-09 16:49:57.284: I/fraglifecycle(1432): recuperando posição da listview: 5
09-09 16:49:57.436: I/fraglifecycle(1432): ChatFragment.onStop();

更改前打印: https://www.dropbox.com/s/upk42kjbvgizoe1/Captura%20de%20tela%202014-09-09%2013.54.07.png?dl=0

更改后打印: https://www.dropbox.com/s/mse9z29zk6ou2nr/Captura%20de%20tela%202014-09-09%2013.52.55.png?dl=0

我的片段:

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

    activity = (MainActivity) getActivity();

    // Em algumas versões do Android, este metódo ocasionará o chamado do
    // metodo onOptionsMenuCreate() imediatamente
    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.chat_layout, container, false);

    edittext = (MyEditText) view.findViewById(R.id.chatinput);
    edittext.setOnEditorActionListener(this);

    listview = (ListView) view.findViewById(R.id.chatlist);

    bt_send = (ImageView) view.findViewById(R.id.sendbutton);
    bt_send.setOnClickListener(this);

    registerForContextMenu(listview);

    // Request service connection

    Intent it = new Intent(activity, MyService.class);

    activity.bindService(it, this, 0);

    return view;
}

@Override
public void onStop() {

    activity.unbindService(this);

    super.onStop();
}

@Override
public void onServiceConnected(ComponentName name, IBinder binderservice) {
          LocalBinder binder = (LocalBinder) binderservice;
    this.service = binder.getService();

    if (this.isChannel()) {
        conversa = service.getCanalAdapter();
    } else {
        String stringuuid = getArguments().getString(EXTRA_ARGUMENT);
        conversa = service.getPVTAdapter(UUID.fromString(stringuuid));
    }

    listview.setAdapter(conversa);

}

请帮助我,我在循环片段 - 服务导向变化方面有几个问题,并且在我的开发中停留了一个多月。 提前致谢!

2 个答案:

答案 0 :(得分:0)

从您的代码和日志中,onStop()很可能是针对上一个活动调用的,而不是新重新创建的活动。

答案 1 :(得分:0)

因为它是碎片:

在片段setRetainInstance(true);中使用onAttach(Activity activity)

即使活动发生变化,也应该停止恢复片段的状态,因此你的片段将是相同的。

我相信应该有所帮助。



Open for correction, as always!

Regards,
Edward Quixote.