創建我在使用容器佈局內片段經理的活動加載到一個片段。在我的活動內部,我開始一項服務以連接到其他藍牙設備,並通過發送和接收某些數據與它進行通信。當應用程序打開並且服務連接到藍牙設備時,一切正常。片段的新實例大幹快上應用啓動
但是,當我打後退按鈕並重新打開我的應用程序,雖然我的服務仍然連接到其它藍牙設備我使用的顯示相同的片段顯示了它在斷開狀態。 我把檢查的文本設置爲使用
fragment.isVisible()
我片段的孩子的TextView之前,它返回false(?)
所以,我想如果我」 m沒有錯,當我每次打開應用程序時,活動都會在原始片段上創建不同的片段實例。
這裏是我活動的onCreate()..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt_dashboard);
System.out.println("OnCreate() of BTDashboardActivity........");
if (findViewById(R.id.fragmentContainer) != null) {
if (savedInstanceState != null) {
System.out.println("ListFragmetn allready exist...");
return;
}
System.out.println("adding listfragment to view...");
listFragment = new BTDashboardListFragment();
listFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, listFragment).commit();
}
}
編輯
這裏是活動在onStart()代碼..
@Override
public void onStart() {
super.onStart();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent,
Constants.REQUEST_ENABLE_BT);
} else {
if (mBluetoothService == null) {
startBluetoothService();
Log.d(TAG, "Chat service [email protected]@@@@@@@@@@@");
showDisconnectedUI();
System.out.println("showing disconnceted status to the user..");
} else {
if (mBluetoothService.getState() == Constants.STATE_CONNECTED) {
System.out.println("showing connection status to the user..");
showConnectedUI();
} else {
System.out.println("showing disconnceted status to the user..");
showDisconnectedUI();
}
}
}
}
這是代碼來設置數據片段的孩子爭奪WS ..
protected void handleResponse(String readMessage) {
System.out.println("response from device: " + readMessage);
if (readMessage != null) {
if (listFragment != null && listFragment.isInLayout()) {
System.out.println("List fragment is found...");
System.out.println("Setting response text to listFragment...");
if(listFragment.isVisible()) // prints not visible after reopening the app
System.out.println("listFragment is visible ...");
else
System.out.println("listFragment is not visible...");
listFragment.setResponseText(readMessage);
}
}
任何幫助表示讚賞..
我認爲,你應該使用藍牙代碼來使用一些檢查。 中,當然,當的onCreate被稱爲片段的新實例將被創建,你是正確的,但因爲服務仍處於活動狀態,其本質爲你把一些檢查這兩個設備之間的「啓用設備配對」狀態。我認爲這可以做出一些改變。 –
感謝您的評論,但blutooth連接不是我的問題。連接保持建立狀態,即使我打後退按鈕並重新打開應用程序。該問題是,我試圖更新片段不再可見。(可能是一個新的實例是越來越置於其上和我」米玩舊的一個是低於新片段) –
Purushottam,我問你保持藍牙檢查,就像如果設備配對再往前......我們能做到嗎? –