onActivityResult for Intent(Settings.ACTION_SETTINGS) called immediately

亡梦爱人 提交于 2020-01-23 10:08:44

问题


In my splash screen I have request to server, but when there's no internet connection i'm opening

Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
startActivityForResult(settingsIntent, REQUEST_ENABLE_CONNECTION);

But problem is that onActivityResult is called immediately with requestCode = REQUEST_ENABLE_CONNECTION

I've also tried to add the flag FLAG_NEW_TASK for the intent with no luck.

The activity is not singleTop or singleInstance in the manifest.

What is the best android solution to resolve this issue? I don't want to use Broadcast as it's not the best way to my flow so it will be taken as last choice for me.

Thanks a lot for the help.


回答1:


If the onActivityForResult isn't working well for you (as @CommonsWare suggested, it's fine) you can create a simple flow that should work fine:

In your activity, add

private boolean isReturnedFromSettings = false;

When you decide there's no internet connection and want to open the settings activity, use startActivity and set isReturnedFromSettings = true;

In your Activity's onResume, add this:

if (isReturnedFromSettings) {
isReturnedFromSettings = false;

//DO WHATEVER
}

Should work...




回答2:


What is the best android solution to resolve this issue?

There is no issue. The activities of the settings app are not documented to support startActivityForResult(), and the main activity (Settings.ACTION_SETTINGS) does not offer it at all.




回答3:


Add this to your intent:

 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);



回答4:


I found a workaround: in my intent

activity.startActivityForResult(intent,REQUEST_SYSTEM_WRITE_PERMISSION);
testDelaydRingtoneSetting(mUri, mCallback);

and here the trick:

private void testDelaydRingtoneSetting(Uri mUri, KrollFunction mCallback) {
        final Uri ringtoneUri = mUri;
        final KrollFunction callback = mCallback;
        final Context context = TiApplication.getInstance().getApplicationContext();
        new android.os.Handler().postDelayed(new Runnable() {
        public void run() {
            if (Settings.System.canWrite(context)) {
                Activity activity = TiApplication.getInstance()
                        .getCurrentActivity();
                activity.finishActivity(REQUEST_SYSTEM_WRITE_PERMISSION);
                setRingtone(ringtoneUri, callback);
            } else
                testDelaydRingtoneSetting(ringtoneUri, callback);
        }
    }, 1000);
}



回答5:


please try this. Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS); startActivity(settingsIntent);



来源:https://stackoverflow.com/questions/22810701/onactivityresult-for-intentsettings-action-settings-called-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!