问题
My activity (MyActivity.class)
executes the method to set the screen lock as follow:
startActivityForResult(Security.setLockscreen(getBaseContext()), 1001);
Then my receiver class logs the change to the screen lock as follow:
public class MyDeviceAdminReceiver extends DeviceAdminReceiver {
@Override
public void onPasswordChanged(Context context, Intent intent) {
super.onPasswordChanged(context, intent);
// pass result back to calling activity
intent = new Intent(context, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("pwdChange", true);
context.startActivity(intent);
}
}
And then I process the result in the onActivityResult
method from MyActivity
class.
Is the above the best way to pass result back to the activity? I noticed the above creates another instance of MyActivity class instead of reusing an existing instance of MyActivity class.
Is there another more efficient way of passing data back to the calling activity?
回答1:
One solution I've found is to use SharedPreferences
to pass data from a DeviceAdminReceiver
class to an Activity
class.
I'm sure there are other working solutions that other experienced developers can post in this thread to share with everyone.
来源:https://stackoverflow.com/questions/13965380/pass-result-from-deviceadminreceiver-back-to-calling-activity