我今天发现了一种奇怪的行为。
我的活动连接到onStart()中的GoogleApiClient并且在onStop()中断开连接
该活动使用GridViewPager来显示我的片段。要通过数据层发送消息,我使用活动和片段之间的回调接口。
如果我从Activity布局中的按钮调用sendMessage(),它可以正常工作。如果片段使用回调接口执行sendMessage(),则sendMessage()显示“未连接”Toast。
在两种方式中,调用Activity中的相同方法,那么它的行为可能有何不同?
我应该提一下,问题只会在第一次重启应用程序后出现。
活动
public class WearPlex extends WearableActivity implements
NavigationRemoteCallbacks,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private List<Node> nodeList = new ArrayList<Node>();
private List<Fragment> fragmentList = new ArrayList<Fragment>();
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_plex);
setAmbientEnabled();
fragmentList.add(NavigationRemoteFragment.getInstance(this));
GridViewPager mGridPager = (GridViewPager)findViewById(R.id.gridViewPager);
mGridPager.setAdapter(new MainGridPageAdapter(getFragmentManager(), fragmentList));
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
protected void onStop() {
googleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
nodeList.clear();
Wearable.NodeApi.getConnectedNodes(googleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) nodeList.add(node);
}
});
}
@Override
public void navigationRemoteSendCommand(String commandPath) {
sendMessage(commandPath, null);
}
public void debugOnClick(View view) {
sendMessage("/debug", null);
}
public void sendMessage(String path, byte[] data) {
if (googleApiClient.isConnected()) {
for (int i = 0; i < nodeList.size(); i++) {
if (nodeList.get(i).isNearby()) {
Toast.makeText(this, "Send message", Toast.LENGTH_SHORT).show();
Wearable.MessageApi.sendMessage(googleApiClient, nodeList.get(i).getId(), path, data);
}
}
} else {
Toast.makeText(this, "Not connected", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this, "Connection failed", Toast.LENGTH_SHORT).show();
}
片段
public class NavigationRemoteFragment extends Fragment {
private static NavigationRemoteFragment navigationRemoteFragment = null;
private NavigationRemoteCallbacks callbackHandler = null;
private ImageButton navBtnCenter;
public static NavigationRemoteFragment getInstance(NavigationRemoteCallbacks handler) {
if (navigationRemoteFragment == null) {
navigationRemoteFragment = new NavigationRemoteFragment();
navigationRemoteFragment.callbackHandler = handler;
}
return navigationRemoteFragment;
}
public NavigationRemoteFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_navigation_remote, container, false);
navBtnCenter = (ImageButton)v.findViewById(R.id.navBtnCenter);
navBtnCenter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callbackHandler.navigationRemoteSendCommand("/debug");
}
});
return v;
}
}
回调界面
public interface NavigationRemoteCallbacks {
public void navigationRemoteSendCommand(String commandPath);
}
为MainGridPageAdapter编辑1代码
public class MainGridPageAdapter extends FragmentGridPagerAdapter {
private List<Fragment> fragmentList = null;
public MainGridPageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getFragment(int i, int i1) {
if (i1 < fragmentList.size()) return fragmentList.get(i1);
return null;
}
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount(int i) {
return fragmentList.size();
}
答案 0 :(得分:1)
您没有显示MainGridPageAdapter
的代码,因此我不知道它是如何管理片段的。您提到重启后问题就出现了。查看WearPlex.onCreate()
中的代码,我怀疑问题是导致片段持有对旧的,被破坏的活动实例的引用。
FragmentManager
的记录不佳的行为是它在重新启动时保存其状态。这经常被忽略,导致重启后重复的片段实例。在主机活动的onCreate()
方法中管理片段创建的正确模式是:
if (savedInstanceState == null) {
// Not a restart
// Create a new instance of the fragment
// Add it to the fragment manager
} else {
// Restart
// The fragment manager has saved and restored the fragment instances
// Use findFragmentById() to get the fragment if you need it
}
您没有在savedInstanceState
中使用onCreate()
来测试重启。重启后,您是否看到了比预期更多的碎片?如果是这样,原始片段将保留对旧活动的引用,该活动已停止,并且具有断开连接的GoogleApiClient。如果单击其中一个片段的NavBtn,您将看到&#34;未连接&#34;烤面包。
<强>更新强>
问题是由您创建NavigationRemoteFragment
的新实例的方式引起的,特别是使用静态成员navigationRemoteFragment
。重新启动后,重新创建活动时,代码会调用NavigationRemoteFragment.getInstance(this)
。 getInstance()
发现navigationRemoteFragment
不为null,因为它是静态的,并且不会创建新的片段。返回的片段是旧片段,它包含对旧活动的引用,该活动已停止并且已断开连接GoogleApiClient
。
这可以通过使用isDestroyed方法并添加一些调试日志记录来确认:
@Override
public void navigationRemoteSendCommand(String commandPath) {
if (isDestroyed()) {
Log.w("TEST", "This is an old instance of the activity");
}
sendMessage(commandPath, null);
}