0

In the onReceive(), I want to create a new Intent and start a new Activity. After the activity finishes, I want to get back to the same line from where I left for the activity. I know that I cannot use BroadcastReceiver class for achieving this. How can this be achieved? Below is the onReceive() code:

final BroadcastReceiver mPairingRequestRecevier = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction()))
        {
            final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);

            if (type == BluetoothDevice.PAIRING_VARIANT_PIN)
            {
                abortBroadcast();
                String passkey="hey";
                Intent passkeyIntent = new Intent(context.getApplicationContext(),TestActivity.class);
                passkeyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                passkeyIntent.putExtra("passkey",passkey);
                context.startActivity(passkeyIntent);
                Log.d("after returning...",passkey);

            }
            else
            {
                Log.d("Unexpected pairingtype:" , type+"");
            }
        }
    }
};

Any idea that can achieve this would be appreciated.

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
  • "After the activity finishes, I want to get back to the same line from where I left for the activity" -- that is not possible. `startActivity()` is asynchronous. No significant steps will have been taken to start the other activity by the time `startActivity()` returns. The real work will not begin until `onReceive()` returns and you return control of the main application thread over to the framework. Your `TestActivity` needs to do whatever the work is that you were considering having the receiver do. – CommonsWare Jul 04 '17 at 11:55
  • What do you means by same line? – Andy Developer Jul 04 '17 at 12:05
  • @CommonsWare Yes you are right. Do you know how can it be achieved then. Maybe I am missing the correct class or method name or some flag. Can you suggest? – Ankit Shubham Jul 04 '17 at 12:09
  • @AndyDeveloper By same line, I mean the line after `context.startActivity(passkeyIntent);` – Ankit Shubham Jul 04 '17 at 12:10
  • "Do you know how can it be achieved then" -- move the affected code inside the activity. The receiver starts the activity *and that's it*. – CommonsWare Jul 05 '17 at 11:04
  • @CommonsWare Yes, I did that exactly. Instead of passing `passkey` in the extras of the intent, I sent the `BluetoothDevice device` as parcelable extra to the `TestActivity` and did whatever I wanted to do with `device`. Actually, earlier my plan was to get the value of the passkey in the `passkey` variable while the control was in `TestActivity` and on calling `finish()`, I get the value from `passkey` and apply that to `device` in the `onReceive` method itself. But now I am passing the `device` itself to the `TestActivity`. I get the passkey value there itself and do what I wanted with them. – Ankit Shubham Jul 05 '17 at 16:09

1 Answers1

0

You can use startActivityFor result instead of startActivity to start an activity..make it perform some task and return the result to previous activity Look at this: How to manage `startActivityForResult` on Android?

Shubham Goel
  • 1,962
  • 17
  • 25