Search code examples
androidkotlinandroid-activityandroid-toast

Displaying Toast after opening a Settings Activity


I'm trying to display a Toast right after I open a Settings Activity, which should contain a few instructions on what to do in the opening screen.

For example, let's say I want the user to connect to a specific network, so in my code I run

    Toast.makeText(context, "Instruction message", Toast.LENGTH_SHORT).show()
    startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))

to display a Toast followed by a call to open up the WiFi Settings Activity. The problem here is that the Toast will display in the current Activity inside my app, but very quickly disappear as soon as the new Activity comes up.

I'm wondering if there's a way of displaying that same Toast but on the opening Activity, rather than the one that is doing the call, in order to properly instruct the user on what to do on such new Activity.

Or perhaps there's a better way of achieving this?


Solution

  • I think it would be better if you used a dialog, so after clicking OK, the user will be shown the screen to change WiFi, like so:

    AlertDialog.Builder(context)
        .setTitle("Your app name")
        .setMessage("Please turn on WiFi on the following screen.")
        .setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id ->
            {
                startActivity(Settings.ACTION_WIFI_SETTINGS)
            }
        })
        .setNegativeButton("Cancel", null)
        .show();
    

    You can read more about Dialogs here: https://developer.android.com/guide/topics/ui/dialogs