本文介绍了机器人:使用片段或更改所有视图中的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我问你一个建议,我的应用程序。我要开发一个应用程序与此特点:

I ask you a suggestion for my application. I have to develop one app with this characteristics :

1只纵向

2 - 它显示的蓝牙设备presents打开应用程序时

2- when open the app it shows the bluetooth devices presents

3,当你点击一台设备上的应用程序要求用户解锁code和显示的连接按钮

3- when you click on one device the app ask to the user an unlock code and show the connect button

4-后preSS连接按钮,该应用程序显示蒙山两点三个按钮加载微调吧

4- after press connect button the app show a loading spinner bar whith two o three buttons

问题的关键是:是否有更好的,我用了三个不同的片段,每一个行为或不

现在我已经做了:

有关扫描设备的一个活动。

one activity for the scan device

有关解锁code一个活动

one activity for the unlock code

但我不知道我在哪里可以把加载屏幕(加载微调酒吧和三个按钮)

but i don't know where I can put the loading screen (loading spinner bar and the three buttons)

现在我想的发展方式不同。一个中心的活动,处理了3种不同的片段加载:

Now I'm thinking of develop in different way. One central activity that handles the loading of 3 different fragments :

  1. 另一个用于扫描设备

  1. one for scan device

一个用于解锁code和一个用于加载屏幕

one for unlock code and one for loading screen

不过,我是新的Andr​​oid的编程,我总是在想,如果我认为正确的方式或以错误的方式。

But I'm new in the Android programming, and I always wonder if I think in correct way or in the wrong way .

而在最后:从片段通讯所选择的设备,我想我会实现mainactivity一个监听活动。是不是?

And in last : for communicate the chosen device from fragment to the activity I think I will implement a listener in mainactivity. Is it right ?

****编辑:**。*我就主要问题的另一个疑问***

****EDIT :** *I have another doubt regarding the main question.***

现在你的建议后,我想开发这个应用程序,以这种方式:

Now after your advice I want develop this app in this way :

  • 主要活动

  • Main Activity

  • 在扫描设备片段
  • 解锁设备片段

  • Scan DEvice fragment
  • Unlock Device fragment

装载片段

在precedent版本,我想开发三种不同的活动,并使用粘结剂和messange在两个方向的方式来/从通信服务

In the precedent version I thought to develop three different activity and to use binder and messange to communicate in two direction way to/from the service

现在,而是有主要活动内三个不同的片段。我的问题是:?对你来说是更好地实施内部mainactivity或每一个片段内的交际到服务

Now,instead, there are three different fragments inside the main activity. My question is : for you is better implement the comunication to the service inside mainactivity or inside every single fragment ?

有关例如:用户选择一个设备在扫描片段,该片段直接通信的的choise到服务或通信的的choise向其中将该信息转发给该服务的mainactivity

感谢您的时间:)

推荐答案

你的不可以使用的setContentView()应用程序的切换状态,它可能会导致在 onBack pressed不一致问题() - 利用片段来代替。但是,你会遇到在背部preSS的一个问题,所以你需要看看是否有留在你的活动上背部preSS,为此,你需要看到的 ContainerFragment在这个问题上。

Do not use setContentView() to change between states of the application, it can cause inconsistency problems on onBackPressed() - use fragments instead. But you will run into a problem on back press, so you will need to see if there are fragments left in your activity on back press, for which you will need to see http://stackoverflow.com/a/24527530/2413303 ContainerFragment in this question.

    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super.onCreate(saveInstanceState);
        this.setContentView(R.layout.activity_container);
        if (saveInstanceState == null)
        {
             getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_container_container, new ExampleFragment())
                .addToBackStack(null)
             .commit();
        }
        getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
        {
            public void onBackStackChanged()
            {
                int backCount = getSupportFragmentManager().getBackStackEntryCount();
                if (backCount == 0)
                {
                    finish();
                }
            }
        });
    }

此外,使用 ButterKnife 库,我并没有在这个例子中,我联系,即使我应该有。它使code少得多冗长,做同样的事情。

Also, use ButterKnife library, I didn't in this example I linked, even though I should have. It makes the code much less verbose and do the same thing.

public class FancyFragment extends Fragment {
   @InjectView(R.id.button1) Button button1;
   @InjectView(R.id.button2) Button button2;

  @OnClick(R.id.btnSubmit)
  public void submit(View view) {
     // TODO submit data to server...
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fancy_fragment, container, false);
     ButterKnife.inject(this, view);
     // TODO Use "injected" views...
     return view;
  }
}

和沟通,您可以使用奥托库按http://stackoverflow.com/a/28480952/2413303

And for communication, you can use Otto library as per http://stackoverflow.com/a/28480952/2413303

public class UpdateListEvent {

}

public class MainActivity extends ActionBarActivity {
    ...
    public void updatelist() {
       SingletonBus.INSTANCE.getBus().post(new UpdateListEvent());
    }
}

public class FragmentA extends Fragment {
    @Override
    public void onResume() {
        super.onResume();
        SingletonBus.INSTANCE.getBus().register(this);
    }

    @Override
    public void onPause() {
        SingletonBus.INSTANCE.getBus().unregister(this);
        super.onPause();
    }

    @Subscribe
    public void onUpdateListEvent(UpdateListEvent e) {
         //do things
    }
}

public enum SingletonBus {
    INSTANCE;

    private Bus bus;

    private SingletonBus() {
        this.bus = new Bus(ThreadEnforcer.ANY);
    }

    public Bus getBus() {
        return bus;
    }
}

这篇关于机器人:使用片段或更改所有视图中的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:40