Note that there are some explanatory texts on larger screens.

plurals
  1. PONew instance of fragment gets created on application launch
    primarykey
    data
    text
    <p>I have a single fragment loaded in an activity using fragment manager inside a container layout. Inside my activity i start a service to connect to other bluetooth device and communicate with it by sending and receiving certain data. Everything works fine when the app is open and the service is connected to bluetooth device. </p> <p>But when i hit back button and reopen my app , though my service is still connected to other bluetooth device the fragment i'm using to display the same shows it is in disconnected state. I put a check before setting the text to my fragment's child textview using </p> <blockquote> <p>fragment.isVisible()</p> </blockquote> <p>and it returned false (?)</p> <p>So , i think if i'm not wrong , the activity is creating a different instance of fragment over my original fragment everytime i open the app. </p> <p>Here is the onCreate() of my activity..</p> <pre><code>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(); } } </code></pre> <p><strong>EDIT</strong></p> <p>here is code for onStart() of activity..</p> <pre><code>@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 Started...@@@@@@@@@@@@"); 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(); } } } </code></pre> <p>}</p> <p>And this is code to set data to fragment's child views..</p> <pre><code>protected void handleResponse(String readMessage) { System.out.println("response from device: " + readMessage); if (readMessage != null) { if (listFragment != null &amp;&amp; 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); } } </code></pre> <p>Any help is appreciated..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload