1

For example: my app calls a native settings activity, by

Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(myIntent, CODE);

Then when it is finished (by pressing the back button for example) I want to know. I tried this way, but the onActivityResult() wasn't called when the native activity was finalized... so my app wasn't informed...

How I have to do? Anyone knows?

Thanks...

EDIT:

Thanks every one. The complete code is that:

public class MainActivity extends AppCompatActivity {

    public static final int DIALOG_CODE = 0x1;

    private TextView txtGps;
    private TextView txtNet;

    private LocationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        first();
    }

    private void checkConfigs() {
        boolean gps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean net = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        setWidgets(gps, net);

        if (gps && net){
            Toast.makeText(this, "Configurações Ok!", Toast.LENGTH_SHORT).show();
        } else {
            DialogConfig.exibDialog(getFragmentManager());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == DIALOG_CODE) && (resultCode == RESULT_OK)){
            checkConfigs();
        } else {
            Toast.makeText(this, "As config não foram alteradas!", Toast.LENGTH_SHORT).show();
        }
    }

    public void setWidgets(boolean gsp, boolean net){
        if (gsp){
            txtGps.setText(R.string.gps_hab);
        } else {
            txtGps.setText(R.string.gps_n_hab);
        }

        if (net){
            txtNet.setText(R.string.wifi_hab);
        } else {
            txtNet.setText(R.string.wifi_n_hab);
        }
    }

    private View.OnClickListener onClickCheck() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkConfigs();
            }
        };
    }

    @SuppressWarnings("ConstantConditions")
    private void first() {
        txtGps = (TextView) findViewById(R.id.txt_gps);
        txtNet = (TextView) findViewById(R.id.txt_net);

        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        findViewById(R.id.btn_check).setOnClickListener(onClickCheck());
    }

    public static class DialogConfig extends DialogFragment {

        public static void exibDialog(FragmentManager fm){
            new DialogConfig().show(fm, "dialog");
        }

        @Override
        public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setTitle("As conf não estão corretas! Alt configs?")
                    .setPositiveButton("Ok", onOk())
                    .setNegativeButton("Cancel", onCancel())
                    .create();
        }

        private DialogInterface.OnClickListener onCancel() {
            return new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();
                }
            };
        }

        private DialogInterface.OnClickListener onOk() {
            return new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(myIntent, DIALOG_CODE);
                }
            };
        }
    }
}

[Resolved]

Thanks again to every one, I tried again, creating a interface of callback in dialog, so the activity could be informed and call the other native settings activity by itself , and got the response in onActivityResult(), but comparing only a code (not by RESULT_OK), This way worked. But there is a other way (like was suggested) that use the lifecycle, to me seems more easy.

Cristina
  • 387
  • 1
  • 18
  • Your activity will be called with the standard lifecycle methods: `onRestart()`, `onStart()`, and `onResume()`. Note that these will be called for other reasons as well (e.g., user pressed HOME and returned). – CommonsWare May 05 '16 at 21:38
  • show your onActivityResult method – Raiv May 05 '16 at 21:41
  • Are you sure that activity should return something to you? – Nanoc May 05 '16 at 21:45
  • maybe this thread can help you: http://stackoverflow.com/questions/11919259/how-to-get-startactivityforresult-on-external-activity-to-work – mbonnin May 05 '16 at 21:51
  • do you handle your onBackPress() Method? if not then override that method and put your code there for returning the result – Rashid May 06 '16 at 00:51
  • @CommonsWare The lifecycle! How have I never thought of this yet? Good! Thanks – Cristina May 06 '16 at 00:56

1 Answers1

0

I basically tried what you tried and it works for me.

startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);

and for onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(PREFIX, "onActivityResult");
}

When I came back to the app, onActivityResult was called. Not sure what you are doing different. can you show your onActivityResult code?

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
  • I will give ur code a try but from a quick look, I recommend not calling `startActivity()` or `startActivityForResult()` from a static class/method. Just to test it, do `startActivityForResult()` from `onResume` and see if it works – ᴛʜᴇᴘᴀᴛᴇʟ May 06 '16 at 01:15