4

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.

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
Udi Oshi
  • 6,787
  • 7
  • 47
  • 65
  • What result code do you get in onActivityResult? RESULT_CANCELLED or anything else? – andyandy Apr 02 '14 at 12:07
  • RESULT_CANCELED but it doesn't really matter because onActivityResult is called, and when returning to the app with back button it doesn't called any more – Udi Oshi Apr 02 '14 at 12:09

5 Answers5

8

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...

Lior Iluz
  • 26,213
  • 16
  • 65
  • 114
  • 3
    This flag would need to be maintained in `onSaveInstanceState` & `onRestoreInstanceState` because you can lose it if system kills your `Activity` when the Settings screen is open. – Bartek Lipinski Jun 15 '19 at 17:21
5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
4

Add this to your intent:

 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
0

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);
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
-1

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

User10001
  • 1,295
  • 2
  • 18
  • 30
  • I need to return to my activity in a proper way to ask if device has connection, this is not good for me at all. – Udi Oshi Apr 02 '14 at 12:02
  • 4
    wt does it mean? ...please explain why you not do this. you can check the (device has connection) in the onResume method of the Activity. – User10001 Apr 02 '14 at 12:13