[Android Software Development] ronnie301184:
I decided to fix up some crummy UI elements in an app that I'm writing (and self-learning to code in the process) and a focus issue has arisen.

Previously I had a menu option appear when my phones menu button was pressed which would either start a request to turn on bluetooth if it was not on, or start a request to turn on bluetooth visibility for 180 seconds if bluetooth was already on. Essentially, if bluetooth was not on, you would have to use the menu button option twice in order to turn it on AND turn BT visibility on.

I've modified my static menu into a dynamic menu so that the menu option text differs, depending on whether BT is on or not. It still requires you to access the menu option twice to turn BT and visibility on (the thread isn't about that, but if anyone knows how to turn this into one step, instead of two, I'm all ears :D )

This issue is that if BT is on, and the menu option asks you to turn on visibility, choosing this option minimises my app (its still running but the homescreen has focus). The actual request to turn visibility on DOES work, but I'm not sure why my app is losing focus. Is there any way to ensure it doesn't lose focus, or to restore focus once the user has chosen to allow or disallow making their device visible?

My menu display code is:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Check if bluetooth is available
if (mBluetoothAdapter == null)	{
// Device does not support Bluetooth
Toast.makeText(PS3mote.this, "Device does not support bluetooth. App is unusable.", Toast.LENGTH_LONG).show();
}
else
// Check if bluetooth is on. If not, show bluetooth option in menu
if (!mBluetoothAdapter.isEnabled()) {
menu
.findItem(R.id.visible)
.setVisible(false)
.setEnabled(false);
menu
.findItem(R.id.bton)
.setVisible(true)
.setEnabled(true);
}
// If bluetooth is on, show visibility option in menu
else {
menu
.findItem(R.id.visible)
.setVisible(true)
.setEnabled(true);
menu
.findItem(R.id.bton)
.setVisible(false)
.setEnabled(false);
}
return true;
}

My menu selection code is:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bton:
bton();
return true;
case R.id.visible:
visible();
case R.id.quit:
quit();
return true;
case R.id.about:
about();
return true;
case R.id.help:
help();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

and my BT and visibility request codes are:

private void bton() {
// Check if bluetooth is available
if (mBluetoothAdapter == null)	{
// Device does not support Bluetooth
Toast.makeText(PS3mote.this, "Device does not support bluetooth. App is unusable.", Toast.LENGTH_LONG).show();
}
else {
// Request to turn on bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}

private void visible() {
// Check if bluetooth is visible. If not visible, then request visibility for 180 seconds
if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 180);
startActivity(discoverableIntent);
}
}

Thanks in advance :)