0

First I think I should describe my problem: I want to do a BluetoothRequest for turning it on, but I don't want to create a new Activity. I want to keep it in the MainActivity and put the code in subclasses. How is this possible? Down is my own idea of how to do it, but it don't works.

I have the MainActivity and a BluetoothActivity. The following code is from BluetoothActivity:

public class Bluetooth extends Fragment{
    private Context context;
    private TextView textView;
    private final int REQUEST_ENABLE_BT = 1;
    private String BtName;
    private String BtAddress;
    private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    public Bluetooth(Context context){
        //constructor
        this.context=context;
        textView =  new TextView(this.context);
    }
    public boolean bluetoothUnterstuetz(){
        if(mBluetoothAdapter == null) {
            //Device doesn't support Bluetooth
            textView.setText("Device doesn't support Bluetooth");
        }
        return true;
    }
    public void bluetoothActivate(){
        if(mBluetoothAdapter.getState()== BluetoothAdapter.STATE_ON){
            textView.setText("Bluetooth already on.");
        }
        if(!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            ((Activity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
        ((Activity) context).setContentView(textView);
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        textView.setText(resultCode);
        Log.i("bluetooth", "called");
        ((Activity) context).setContentView(textView);
    }

With extends Fragment nothing happens, also logcat is empty. With extends Activity, the app crashs after starting. When I test the App on my Phone, I get the BluetoothRequest and can Choose my answer but afterwards the ContentView shows nothing, also Logcat stays empty

Somebody knows how to get the Result?

phenixclaw
  • 35
  • 2
  • 10
  • Possible duplicate of [onActivityResult doesn't work?](http://stackoverflow.com/questions/1984233/onactivityresult-doesnt-work) – Bö macht Blau Oct 18 '15 at 16:59

4 Answers4

1

Well now it works. The onActivityResult() has to be in the MainActivity. If I put it into the subclass it won't be called

phenixclaw
  • 35
  • 2
  • 10
0

You have forgotten tag override.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}
juankirr
  • 323
  • 2
  • 13
0

AndroidStudio tells me the function onActivityResult is never used.

Using the annotation @Override will make this warning to disappear. It is also a good way to check you overrided properly (with the right parameters in the right order) as you will get an error should the override be wrong. Which doesn't look to be the case here if you extended Activity.

When I test the App on my Phone, I get the BluetoothRequest and can Choose my answer but afterwards the ContentView shows nothing.

Are you sure your startActivityForResult() was called and returned a result (ie. didn't call finish() for example)? I'll update my answer according to the details you will provide.

Shlublu
  • 10,917
  • 4
  • 51
  • 70
  • Yes `startActivityForResult()` is called, because by uncomment the row I don't get the Request. It should deliver a result, but i'm not able to call the `onActivityResult()` function – phenixclaw Oct 25 '15 at 12:18
  • @phenixclaw You don't have to call onActivityResult(). The system does it for you once the Activity you have called has finished. – Shlublu Oct 25 '15 at 13:14
  • Well if I don't open a second Activity and I'm just using the Mainactivity it won't work? I need to open a second Activity just for the Request? – phenixclaw Oct 25 '15 at 13:21
0

onActivityResult is a protected method of Activity and a public method of Fragment. If ide is warning you that the method is never used is because your class does not extend neither Activity nor Fragment

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • yes I don't had used Fragment and Activity. With activity the app crashed with starting the app, now with Fragment the warning no longer exists, but onActivityResult is not called. As I understood the declaration, [link](http://developer.android.com/guide/topics/connectivity/bluetooth.html) the onActivityResult should be called automatically "The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult() implementation as the requestCode parameter." @Shlublu – phenixclaw Oct 19 '15 at 08:32
  • update your question. It is almost impossible to understand what's going on without seeing the code – Blackbelt Oct 19 '15 at 08:35